Ver Fonte

Merge pull request #383 from deltachat/certificate_checks

Certificate checks
björn petersen há 5 anos atrás
pai
commit
d2ab0a1f71

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -82,6 +82,7 @@
 		3060119C22DDE24000C1CE6F /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3060119E22DDE24000C1CE6F /* Localizable.strings */; };
 		306011B622E5E7FB00C1CE6F /* Localizable.stringsdict in Resources */ = {isa = PBXBuildFile; fileRef = 306011B422E5E7FB00C1CE6F /* Localizable.stringsdict */; };
 		30A4D9AE2332672700544344 /* QrInviteViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30A4D9AD2332672600544344 /* QrInviteViewController.swift */; };
+		30C0D49D237C4908008E2A0E /* CertificateCheckController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30C0D49C237C4908008E2A0E /* CertificateCheckController.swift */; };
 		30F9B9EC235F2116006E7ACF /* MessageCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30F9B9EB235F2116006E7ACF /* MessageCounter.swift */; };
 		6795F63A82E94FF7CD2CEC0F /* Pods_deltachat_iosTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F7009234DB9408201A6CDCB /* Pods_deltachat_iosTests.framework */; };
 		7070FB9B2101ECBB000DC258 /* GroupNameController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7070FB9A2101ECBB000DC258 /* GroupNameController.swift */; };
@@ -289,6 +290,7 @@
 		306011C822E5E83100C1CE6F /* zh-Hant-TW */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = "zh-Hant-TW"; path = "zh-Hant-TW.lproj/Localizable.stringsdict"; sourceTree = "<group>"; };
 		306011C922E5E83500C1CE6F /* uk */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = uk; path = uk.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
 		30A4D9AD2332672600544344 /* QrInviteViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QrInviteViewController.swift; sourceTree = "<group>"; };
+		30C0D49C237C4908008E2A0E /* CertificateCheckController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CertificateCheckController.swift; sourceTree = "<group>"; };
 		30F9B9EB235F2116006E7ACF /* MessageCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageCounter.swift; sourceTree = "<group>"; };
 		6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_deltachat_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		7070FB9A2101ECBB000DC258 /* GroupNameController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupNameController.swift; sourceTree = "<group>"; };
@@ -621,6 +623,7 @@
 			isa = PBXGroup;
 			children = (
 				AE18F291228C17BC0007B1BE /* PortSettingsController.swift */,
+				30C0D49C237C4908008E2A0E /* CertificateCheckController.swift */,
 				AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */,
 			);
 			path = AccountSetup;
@@ -1115,6 +1118,7 @@
 				305961CD2346125100C80F33 /* UIEdgeInsets+Extensions.swift in Sources */,
 				305962032346125100C80F33 /* CellSizeCalculator.swift in Sources */,
 				305961E02346125100C80F33 /* MessageLabelDelegate.swift in Sources */,
+				30C0D49D237C4908008E2A0E /* CertificateCheckController.swift in Sources */,
 				305961D92346125100C80F33 /* ContactItem.swift in Sources */,
 				305961FB2346125100C80F33 /* TypingIndicator.swift in Sources */,
 				7092474120B3869500AF8799 /* ContactDetailViewController.swift in Sources */,

+ 122 - 0
deltachat-ios/Controller/AccountSetup/CertificateCheckController.swift

@@ -0,0 +1,122 @@
+import UIKit
+
+class CertificateCheckController: UITableViewController {
+
+    var options = [Int(DC_CERTCK_AUTO),
+                   Int(DC_CERTCK_STRICT),
+                   Int(DC_CERTCK_ACCEPT_INVALID_HOSTNAMES),
+                   Int(DC_CERTCK_ACCEPT_INVALID_CERTIFICATES)]
+
+    var currentValue: Int
+    var selectedIndex: Int?
+
+    var okButton: UIBarButtonItem {
+        let button =  UIBarButtonItem(title: String.localized("ok"), style: .done, target: self, action: #selector(okButtonPressed))
+        return button
+    }
+
+    var cancelButton: UIBarButtonItem {
+        let button =  UIBarButtonItem(title: String.localized("cancel"), style: .plain, target: self, action: #selector(cancelButtonPressed))
+        return button
+    }
+
+    var staticCells: [UITableViewCell] {
+        return options.map({
+            let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
+            cell.textLabel?.text =  ValueConverter.convertHexToString(value: $0)
+            cell.selectionStyle = .none
+            return cell
+        })
+    }
+
+    init(sectionTitle: String?) {
+        self.currentValue = DcConfig.certificateChecks
+        for (index, value) in options.enumerated() where currentValue == value {
+            selectedIndex = index
+        }
+        super.init(style: .grouped)
+        self.title = sectionTitle
+    }
+
+    required init?(coder aDecoder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    override func viewDidLoad() {
+        super.viewDidLoad()
+
+        navigationItem.rightBarButtonItem = okButton
+        navigationItem.leftBarButtonItem = cancelButton
+    }
+
+    // MARK: - Table view data source
+
+    override func numberOfSections(in tableView: UITableView) -> Int {
+        return 1
+    }
+
+    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+        return options.count
+    }
+
+    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        if indexPath.section == 0 {
+            let row = indexPath.row
+            selectItem(at: row)
+        }
+    }
+
+    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+        let row = indexPath.row
+        let cell = staticCells[row]
+        if row == selectedIndex || cell.textLabel?.text == "\(currentValue)" {
+            cell.accessoryType = .checkmark
+        } else {
+            cell.accessoryType = .none
+        }
+        return cell
+    }
+
+    private func selectItem(at index: Int? ) {
+        // unselect old cell
+        // select new cell
+        // update value
+        if let oldIndex = selectedIndex {
+            let cell = tableView.cellForRow(at: IndexPath.init(row: oldIndex, section: 0))
+            cell?.accessoryType = .none
+        }
+        if let newIndex = index {
+            // activate accesoryType on selected cell
+            let cell = tableView.cellForRow(at: IndexPath.init(row: newIndex, section: 0))
+            cell?.accessoryType = .checkmark
+            currentValue = options[newIndex]
+        }
+        selectedIndex = index
+    }
+
+    @objc private func okButtonPressed() {
+        DcConfig.certificateChecks = currentValue
+        navigationController?.popViewController(animated: true)
+    }
+
+    @objc private func cancelButtonPressed() {
+        navigationController?.popViewController(animated: true)
+    }
+
+   class ValueConverter {
+        static func convertHexToString(value: Int) -> String {
+            switch value {
+            case Int(DC_CERTCK_AUTO):
+                return String.localized("automatic")
+            case Int(DC_CERTCK_STRICT):
+                return String.localized("strict")
+            case Int(DC_CERTCK_ACCEPT_INVALID_HOSTNAMES):
+                return String.localized("accept_invalid_hostnames")
+            case Int(DC_CERTCK_ACCEPT_INVALID_CERTIFICATES):
+                return String.localized("accept_invalid_certificates")
+            default:
+                return "Undefined"
+            }
+        }
+    }
+}

+ 21 - 5
deltachat-ios/Controller/AccountSetupController.swift

@@ -24,9 +24,10 @@ class AccountSetupController: UITableViewController {
     private let tagSmtpPortCell = 9
     private let tagSmtpPasswordCell = 10
     private let tagSmtpSecurityCell = 11
-    private let tagEmptyServerCell = 12
-    private let tagDeleteAccountCell = 13
-    private let tagRestoreCell = 14
+    private let tagCertCheckCell = 12
+    private let tagEmptyServerCell = 13
+    private let tagDeleteAccountCell = 14
+    private let tagRestoreCell = 15
 
     private let tagTextFieldEmail = 100
     private let tagTextFieldPassword = 200
@@ -57,7 +58,8 @@ class AccountSetupController: UITableViewController {
         smtpUserCell,
         smtpPortCell,
         smtpPasswordCell,
-        smtpSecurityCell
+        smtpSecurityCell,
+        certCheckCell
     ]
     private lazy var folderCells: [UITableViewCell] = [inboxWatchCell, sentboxWatchCell, mvboxWatchCell, sendCopyToSelfCell, mvboxMoveCell]
     private lazy var dangerCells: [UITableViewCell] = [emptyServerCell, deleteAccountCell]
@@ -274,6 +276,17 @@ class AccountSetupController: UITableViewController {
         return cell
     }()
 
+    lazy var certCheckCell: UITableViewCell = {
+        let certCheckType = CertificateCheckController.ValueConverter.convertHexToString(value: DcConfig.certificateChecks)
+        let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
+        cell.textLabel?.text = String.localized("login_certificate_checks")
+        cell.detailTextLabel?.text = certCheckType
+        cell.tag = tagCertCheckCell
+        cell.accessoryType = .disclosureIndicator
+        cell.selectionStyle = .none
+        return cell
+    }()
+
     lazy var inboxWatchCell: SwitchCell = {
         return SwitchCell(textLabel: String.localized("pref_watch_inbox_folder"),
                           on: dcContext.getConfigBool("inbox_watch"),
@@ -468,6 +481,8 @@ class AccountSetupController: UITableViewController {
             coordinator?.showImapSecurityOptions()
         case tagSmtpSecurityCell:
             coordinator?.showSmptpSecurityOptions()
+        case tagCertCheckCell:
+            coordinator?.showCertCheckOptions()
         default:
             break
         }
@@ -753,7 +768,8 @@ class AccountSetupController: UITableViewController {
         smtpPortCell.detailTextLabel?.text = DcConfig.sendPort ?? DcConfig.configuredSendPort
         imapPortCell.detailTextLabel?.text = DcConfig.mailPort ?? DcConfig.configuredMailPort
         smtpSecurityCell.detailTextLabel?.text = SecurityConverter.convertHexToString(type: .SMTPSecurity, hex: DcConfig.getSmtpSecurity())
-        imapSecurityCell.detailTextLabel?.text  = SecurityConverter.convertHexToString(type: .IMAPSecurity, hex: DcConfig.getImapSecurity())
+        imapSecurityCell.detailTextLabel?.text = SecurityConverter.convertHexToString(type: .IMAPSecurity, hex: DcConfig.getImapSecurity())
+        certCheckCell.detailTextLabel?.text = CertificateCheckController.ValueConverter.convertHexToString(value: DcConfig.certificateChecks)
     }
 
     private func resignFirstResponderOnAllCells() {

+ 5 - 0
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -198,6 +198,11 @@ class AccountSetupCoordinator: Coordinator {
         self.navigationController = navigationController
     }
 
+    func showCertCheckOptions() {
+        let certificateCheckController = CertificateCheckController(sectionTitle: String.localized("login_certificate_checks"))
+        navigationController.pushViewController(certificateCheckController, animated: true)
+    }
+
     func showImapPortOptions() {
         let currentMailPort = DcConfig.mailPort ?? DcConfig.configuredMailPort
         let currentPort = Int(currentMailPort)

+ 14 - 0
deltachat-ios/DC/Wrapper.swift

@@ -246,6 +246,20 @@ class DcConfig {
         get { return getConfig("send_port") }
     }
 
+    class var certificateChecks: Int {
+        set {
+            setConfig("smtp_certificate_checks", "\(newValue)")
+            setConfig("imap_certificate_checks", "\(newValue)")
+        }
+        get {
+            if let str = getConfig("imap_certificate_checks") {
+                return Int(str) ?? 0
+            } else {
+                return 0
+            }
+        }
+    }
+
     private class var serverFlags: Int {
         // IMAP-/SMTP-flags as a combination of DC_LP flags
         set {