// // Encryption.swift // NotificationService // // Created by Djorkaeff Alexandre Vilela Pereira on 9/11/20. // Copyright © 2020 Rocket.Chat. All rights reserved. // import Foundation import CommonCrypto import class react_native_simple_crypto.RCTRsaUtils final class Encryption { final var roomKey: String? = nil final var keyId: String? = nil private let privateKey: String? private let credentials: Credentials? private let server: String private let rid: String private var userKey: String? { if let userKey = self.privateKey { guard let json = try? JSONSerialization.jsonObject(with: userKey.data(using: .utf8)!, options: []) as? [String: Any] else { return nil } let utils = RCTRsaUtils() let k = NSMutableDictionary(dictionary: json) return utils.importKey(jwk: k) } return nil } private final let encoder = JSONEncoder() init(server: String, rid: String) { self.privateKey = Storage.shared.getPrivateKey(server: server) self.credentials = Storage.shared.getCredentials(server: server) self.server = server self.rid = rid if let E2EKey = Database(server: server).readRoomEncryptionKey(rid: rid) { self.roomKey = decryptRoomKey(E2EKey: E2EKey) } } func decryptRoomKey(E2EKey: String) -> String? { if let userKey = userKey { let index = E2EKey.index(E2EKey.startIndex, offsetBy: 12) let roomKey = String(E2EKey[index...]) keyId = String(E2EKey[.. String? { if let roomKey = self.roomKey { let index = message.index(message.startIndex, offsetBy: 12) let msg = String(message[index...]) if let data = msg.toData() { let iv = data.subdata(in: 0.. String { if let userId = credentials?.userId, let roomKey = roomKey { let m = Message(_id: id, text: message, userId: userId) let iv = Data.randomBytes(length: kCCBlockSizeAES128) let cypher = try? encoder.encode(m) if let keyId = keyId, let cypher = cypher, let data = Aes.aes128CBC("encrypt", data: cypher, key: roomKey, iv: Shared.toHex(iv)) { let joined = Data.join(vector: iv, data: data) return keyId + joined.base64EncodedString() } } return message } }