Skip to content

Commit 3b4bfa2

Browse files
NMC 1937 - E2EE customisation changes
1 parent 4ea40c8 commit 3b4bfa2

3 files changed

Lines changed: 214 additions & 105 deletions

File tree

Nextcloud.xcodeproj/project.pbxproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@
8686
AFCE353927E5DE0500FEA6C2 /* Shareable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AFCE353827E5DE0400FEA6C2 /* Shareable.swift */; };
8787
CB3666201AF7550816B5CD6A /* NCContextMenuComment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8932E90EC4278026D86CCCC9 /* NCContextMenuComment.swift */; };
8888
AFCE353927E5DE0500FEA6C2 /* NCShare+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AFCE353827E5DE0400FEA6C2 /* NCShare+Helper.swift */; };
89-
B568C2C92DA7CE560072FCB4 /* E2EETests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B568C2C82DA7CE560072FCB4 /* E2EETests.swift */; };
90-
C04E2F232A17BB4D001BAD85 /* FilesIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04E2F222A17BB4D001BAD85 /* FilesIntegrationTests.swift */; };
89+
B529E40D2E8D1F8B006D9131 /* NCManageE2EE.swift in Sources */ = {isa = PBXBuildFile; fileRef = B529E40C2E8D1F8B006D9131 /* NCManageE2EE.swift */; };
9190
D575039F27146F93008DC9DC /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A0D1342591FBC5008F8A13 /* String+Extension.swift */; };
9291
D5B6AA7827200C7200D49C24 /* NCActivityTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B6AA7727200C7200D49C24 /* NCActivityTableViewCell.swift */; };
9392
F310B1EF2BA862F1001C42F5 /* NCViewerMedia+VisionKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = F310B1EE2BA862F1001C42F5 /* NCViewerMedia+VisionKit.swift */; };
@@ -1268,6 +1267,7 @@
12681267
B4C7A5B36D1ED178FB6B76CB /* NCContextMenuPlayerTracks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCContextMenuPlayerTracks.swift; sourceTree = "<group>"; };
12691268
BB7697C94BA14450A0867940 /* NCContextMenuProfile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCContextMenuProfile.swift; sourceTree = "<group>"; };
12701269
AFCE353827E5DE0400FEA6C2 /* NCShare+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NCShare+Helper.swift"; sourceTree = "<group>"; };
1270+
B529E40C2E8D1F8B006D9131 /* NCManageE2EE.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCManageE2EE.swift; sourceTree = "<group>"; };
12711271
B568C2C82DA7CE560072FCB4 /* E2EETests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = E2EETests.swift; sourceTree = "<group>"; };
12721272
C0046CDA2A17B98400D87C9D /* NextcloudUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NextcloudUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
12731273
C04E2F202A17BB4D001BAD85 /* NextcloudIntegrationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NextcloudIntegrationTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -2094,7 +2094,6 @@
20942094
F34BDB3B2F574A58007A222C /* BidiSafeFilenameTests.swift */,
20952095
AA52EB452D42AC5A0089C348 /* Placeholder.swift */,
20962096
B568C2C82DA7CE560072FCB4 /* E2EETests.swift */,
2097-
AF8ED1FB2757821000B8DBC4 /* NextcloudUnitTests.swift */,
20982097
);
20992098
path = NextcloudUnitTests;
21002099
sourceTree = "<group>";
@@ -2413,7 +2412,7 @@
24132412
AF2D7C7D2742559100ADF566 /* NCShareUserCell.swift */,
24142413
AF93471727E2361E002537EE /* NCShareHeader.xib */,
24152414
AF93471527E2361E002537EE /* NCShareHeader.swift */,
2416-
AFCE353827E5DE0400FEA6C2 /* Shareable.swift */,
2415+
AFCE353827E5DE0400FEA6C2 /* NCShare+Helper.swift */,
24172416
AA8E03D92D2ED83300E7E89C /* TransientShare.swift */,
24182417
);
24192418
path = Share;
@@ -2612,6 +2611,7 @@
26122611
F76882042C0DD1E7001CF441 /* Settings */ = {
26132612
isa = PBXGroup;
26142613
children = (
2614+
B529E40C2E8D1F8B006D9131 /* NCManageE2EE.swift */,
26152615
F768820B2C0DD1E7001CF441 /* Settings */,
26162616
F76882162C0DD1E7001CF441 /* AutoUpload */,
26172617
F389C9F32CEE381E00049762 /* SelectAlbum */,
@@ -4649,6 +4649,8 @@
46494649
F704B5E92430C0B800632F5F /* NCCreateFormUploadConflictCell.swift in Sources */,
46504650
F72D404923D2082500A97FD0 /* NCViewerDirectEditing.swift in Sources */,
46514651
AFCE353927E5DE0500FEA6C2 /* Shareable.swift in Sources */,
4652+
F72D404923D2082500A97FD0 /* NCViewerNextcloudText.swift in Sources */,
4653+
AFCE353927E5DE0500FEA6C2 /* NCShare+Helper.swift in Sources */,
46524654
F77BB746289984CA0090FC19 /* UIViewController+Extension.swift in Sources */,
46534655
F700510522DF6A89003A3356 /* NCShare.swift in Sources */,
46544656
F72D1007210B6882009C96B7 /* NCPushNotificationEncryption.m in Sources */,
@@ -4902,6 +4904,7 @@
49024904
AF2D7C7E2742559100ADF566 /* NCShareUserCell.swift in Sources */,
49034905
AF4BF614275629E20081CEEF /* NCManageDatabase+Account.swift in Sources */,
49044906
F76340FA2EBDE9760056F538 /* NCManageDatabaseCore.swift in Sources */,
4907+
B529E40D2E8D1F8B006D9131 /* NCManageE2EE.swift in Sources */,
49054908
F3E173C02C9B1067006D177A /* AwakeMode.swift in Sources */,
49064909
F7CAFE182F164B9500DB35A5 /* NCCollectionViewCommon+CellDelegate.swift in Sources */,
49074910
F711A4DC2AF92CAE00095DD8 /* NCUtility+Date.swift in Sources */,

iOSClient/Settings/E2EE/NCEndToEndInitialize.swift

Lines changed: 41 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
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

524
import UIKit
625
import 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

Comments
 (0)