1- // SPDX-FileCopyrightText: Nextcloud GmbH
2- // SPDX-FileCopyrightText: 2017 Marino Faggiana
3- // SPDX-License-Identifier: GPL-3.0-or-later
1+ //
2+ // NCEndToEndInitialize.swift
3+ // Nextcloud
4+ //
5+ // Created by Marino Faggiana on 03/04/17.
6+ // Copyright © 2017 Marino Faggiana. All rights reserved.
7+ //
8+ // Author Marino Faggiana <marino.faggiana@nextcloud.com>
9+ //
10+ // This program is free software: you can redistribute it and/or modify
11+ // it under the terms of the GNU General Public License as published by
12+ // the Free Software Foundation, either version 3 of the License, or
13+ // (at your option) any later version.
14+ //
15+ // This program is distributed in the hope that it will be useful,
16+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
17+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+ // GNU General Public License for more details.
19+ //
20+ // You should have received a copy of the GNU General Public License
21+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
22+ //
423
524import UIKit
625import NextcloudKit
@@ -28,32 +47,21 @@ class NCEndToEndInitialize: NSObject {
2847 self . metadata = metadata
2948
3049 // Clear all keys
31- NCPreferences ( ) . clearAllKeysEndToEnd ( account: session. account)
50+ NCKeychain ( ) . clearAllKeysEndToEnd ( account: session. account)
3251 self . getPublicKey ( )
3352 }
3453
3554 func statusOfService( session: NCSession . Session , completion: @escaping ( _ error: NKError ? ) -> Void ) {
36- NextcloudKit . shared. getE2EECertificate ( account: session. account) { task in
37- Task {
38- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: session. account,
39- name: " getE2EECertificate " )
40- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
41- }
42- } completion: { _, _, _, _, error in
55+ NextcloudKit . shared. getE2EECertificate ( account: session. account) { _, _, _, _, error in
4356 completion ( error)
4457 }
4558 }
4659
4760 private func getPublicKey( ) {
48- NextcloudKit . shared. getE2EECertificate ( account: session. account) { task in
49- Task {
50- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: self . session. account,
51- name: " getE2EECertificate " )
52- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
53- }
54- } completion: { account, certificate, _, _, error in
61+
62+ NextcloudKit . shared. getE2EECertificate ( account: session. account) { account, certificate, _, _, error in
5563 if error == . success, let certificate {
56- NCPreferences ( ) . setEndToEndCertificate ( account: account, certificate: certificate)
64+ NCKeychain ( ) . setEndToEndCertificate ( account: account, certificate: certificate)
5765 self . extractedPublicKey = NCEndToEndEncryption . shared ( ) . extractPublicKey ( fromCertificate: certificate)
5866 // Request PrivateKey chiper to Server
5967 self . getPrivateKeyCipher ( )
@@ -70,21 +78,15 @@ class NCEndToEndInitialize: NSObject {
7078 return
7179 }
7280
73- NextcloudKit . shared. signE2EECertificate ( certificate: csr, account: account) { task in
74- Task {
75- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: account,
76- name: " signE2EECertificate " )
77- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
78- }
79- } completion: { account, certificate, _, error in
81+ NextcloudKit . shared. signE2EECertificate ( certificate: csr, account: account) { account, certificate, _, error in
8082 if error == . success, let certificate {
8183 // TEST publicKey
8284 let extractedPublicKey = NCEndToEndEncryption . shared ( ) . extractPublicKey ( fromCertificate: certificate)
8385 if extractedPublicKey != NCEndToEndEncryption . shared ( ) . generatedPublicKey {
8486 let error = NKError ( errorCode: error. errorCode, errorDescription: " error: the public key is incorrect " )
8587 NCContentPresenter ( ) . messageNotification ( " E2E sign publicKey " , error: error, delay: NCGlobal . shared. dismissAfterSecond, type: NCContentPresenter . messageType. error, priority: . max)
8688 } else {
87- NCPreferences ( ) . setEndToEndCertificate ( account: account, certificate: certificate)
89+ NCKeychain ( ) . setEndToEndCertificate ( account: account, certificate: certificate)
8890 // Request PrivateKey chiper to Server
8991 self . getPrivateKeyCipher ( )
9092 }
@@ -111,59 +113,30 @@ class NCEndToEndInitialize: NSObject {
111113 }
112114 }
113115
114- func detectPrivateKeyFormat( from data: Data ) -> String {
115- print ( " 🔍 Hex dump: " , data. prefix ( 32 ) . map { String ( format: " %02X " , $0) } . joined ( separator: " " ) )
116-
117- // PKCS#8 has OBJECT IDENTIFIER for RSA
118- let oidRsaPrefix : [ UInt8 ] = [ 0x30 , 0x0D , 0x06 , 0x09 , 0x2A , 0x86 , 0x48 , 0x86 , 0xF7 , 0x0D , 0x01 , 0x01 , 0x01 ]
119-
120- if data. range ( of: Data ( oidRsaPrefix) ) != nil {
121- print ( " 🔐 Format: PKCS#8 (BEGIN PRIVATE KEY) " )
122- return " PKCS#8 "
123- } else if data. starts ( with: [ 0x30 , 0x82 ] ) {
124- print ( " 🔐 Format: PKCS#1 (BEGIN RSA PRIVATE KEY) " )
125- return " PKCS#1 "
126- } else {
127- print ( " ❌ Unknown key format " )
128- return " Unknown "
129- }
130- }
131-
132116 private func getPrivateKeyCipher( ) {
133117 // Request PrivateKey chiper to Server
134- NextcloudKit . shared. getE2EEPrivateKey ( account: session. account) { task in
135- Task {
136- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: self . session. account,
137- name: " getE2EEPrivateKey " )
138- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
139- }
140- } completion: { account, privateKeyChiper, _, error in
118+ NextcloudKit . shared. getE2EEPrivateKey ( account: session. account) { account, privateKeyChiper, _, error in
141119 if error == . success {
142120 // request Passphrase
143121 var passphraseTextField : UITextField ?
144122 let alertController = UIAlertController ( title: NSLocalizedString ( " _e2e_passphrase_request_title_ " , comment: " " ) , message: NSLocalizedString ( " _e2e_passphrase_request_message_ " , comment: " " ) , preferredStyle: . alert)
145123 let ok = UIAlertAction ( title: " OK " , style: . default, handler: { _ in
146124 let passphrase = passphraseTextField? . text ?? " "
147- if let privateKeyData = NCEndToEndEncryption . shared ( ) . decryptPrivateKey ( privateKeyChiper, passphrase: passphrase) ,
125+ let publicKey = NCKeychain ( ) . getEndToEndCertificate ( account: account)
126+ if let privateKeyData = ( NCEndToEndEncryption . shared ( ) . decryptPrivateKey ( privateKeyChiper, passphrase: passphrase) ) ,
148127 let keyData = Data ( base64Encoded: privateKeyData) ,
149128 let privateKey = String ( data: keyData, encoding: . utf8) {
150- NCPreferences ( ) . setEndToEndPrivateKey ( account: account, privateKey: privateKey)
129+ NCKeychain ( ) . setEndToEndPrivateKey ( account: account, privateKey: privateKey)
151130 } else {
131+
152132 let error = NKError ( errorCode: NCGlobal . shared. errorInternalError, errorDescription: NSLocalizedString ( " _e2e_error_incorrect_passphrase_ " , comment: " " ) )
153133 NCContentPresenter ( ) . messageNotification ( NSLocalizedString ( " _e2e_error_passphrase_title " , comment: " " ) , error: error, delay: NCGlobal . shared. dismissAfterSecond, type: NCContentPresenter . messageType. error, priority: . max)
154-
155134 return
156135 }
157136 // Save to keychain
158- NCPreferences ( ) . setEndToEndPassphrase ( account: account, passphrase: passphrase)
137+ NCKeychain ( ) . setEndToEndPassphrase ( account: account, passphrase: passphrase)
159138 // request server publicKey
160- NextcloudKit . shared. getE2EEPublicKey ( account: account) { task in
161- Task {
162- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: self . session. account,
163- name: " getE2EEPublicKey " )
164- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
165- }
166- } completion: { account, publicKey, _, error in
139+ NextcloudKit . shared. getE2EEPublicKey ( account: account) { account, publicKey, _, error in
167140 if error == . success, let publicKey {
168141
169142 // Verify Certificate
@@ -205,7 +178,7 @@ class NCEndToEndInitialize: NSObject {
205178
206179 alertController. addAction ( ok)
207180 alertController. addAction ( cancel)
208- alertController. addTextField { textField in
181+ alertController. addTextField { textField -> Void in
209182 passphraseTextField = textField
210183 passphraseTextField? . placeholder = NSLocalizedString ( " _enter_passphrase_ " , comment: " " )
211184 }
@@ -253,26 +226,14 @@ class NCEndToEndInitialize: NSObject {
253226 // privateKeyChiper
254227 print ( privateKeyCipher)
255228
256- NextcloudKit . shared. storeE2EEPrivateKey ( privateKey: privateKeyCipher, account: session. account) { task in
257- Task {
258- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: self . session. account,
259- name: " storeE2EEPrivateKey " )
260- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
261- }
262- } completion: { account, _, _, error in
229+ NextcloudKit . shared. storeE2EEPrivateKey ( privateKey: privateKeyCipher, account: session. account) { account, _, _, error in
263230 if error == . success, let privateKey = privateKeyString {
264231
265- NCPreferences ( ) . setEndToEndPrivateKey ( account: account, privateKey: String ( privateKey) )
266- NCPreferences ( ) . setEndToEndPassphrase ( account: account, passphrase: e2ePassphrase)
232+ NCKeychain ( ) . setEndToEndPrivateKey ( account: account, privateKey: String ( privateKey) )
233+ NCKeychain ( ) . setEndToEndPassphrase ( account: account, passphrase: e2ePassphrase)
267234
268235 // request server publicKey
269- NextcloudKit . shared. getE2EEPublicKey ( account: account) { task in
270- Task {
271- let identifier = await NCNetworking . shared. networkingTasks. createIdentifier ( account: self . session. account,
272- name: " getE2EEPublicKey " )
273- await NCNetworking . shared. networkingTasks. track ( identifier: identifier, task: task)
274- }
275- } completion: { account, publicKey, _, error in
236+ NextcloudKit . shared. getE2EEPublicKey ( account: account) { account, publicKey, _, error in
276237 if error == . success, let publicKey {
277238
278239 var verifyCertificate : Bool = false
0 commit comments