From 72dc146ff20a1f2ddbb568f050807efcbddcff37 Mon Sep 17 00:00:00 2001 From: Chris Tomlinson Date: Wed, 12 Jul 2023 12:39:10 +0100 Subject: [PATCH] wip --- .../KdbxSwift/DatabaseFileManager.swift | 8 ++++---- .../Sources/KdbxSwift/base32/Base32.swift | 3 ++- .../Sources/KdbxSwift/db/Database.swift | 18 +++++++++--------- .../Sources/KdbxSwift/db/DatabaseItem.swift | 4 +++- ios/KdbxSwift/Sources/KdbxSwift/db/Entry.swift | 7 ++++--- ios/KdbxSwift/Sources/KdbxSwift/db/Group.swift | 7 ++++--- .../Sources/KdbxSwift/db/KeyHelper.swift | 6 +++--- .../KdbxSwift/db/cipher/DataCipher.swift | 3 ++- .../Sources/KdbxSwift/db/kdf/Argon2KDF.swift | 7 ++++--- .../Sources/KdbxSwift/db/kp2/Database2.swift | 4 ++-- .../Sources/KdbxSwift/db/kp2/Header2.swift | 4 ++-- .../Sources/KdbxSwift/util/Extensions.swift | 3 ++- .../Sources/KdbxSwift/util/Logger.swift | 5 +++++ 13 files changed, 46 insertions(+), 33 deletions(-) diff --git a/ios/KdbxSwift/Sources/KdbxSwift/DatabaseFileManager.swift b/ios/KdbxSwift/Sources/KdbxSwift/DatabaseFileManager.swift index 1995c1d..564c43b 100644 --- a/ios/KdbxSwift/Sources/KdbxSwift/DatabaseFileManager.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/DatabaseFileManager.swift @@ -66,12 +66,12 @@ public class DatabaseFileManager { } catch { Logger.mainLog.error("Failed to read current KDBX file [message: \(error.localizedDescription)]") - fatalError("couldn't read KDBX file") + Logger.fatalError("couldn't read KDBX file") } } guard let db = initDatabase(signature: fileData) else { - fatalError("database init failed") + Logger.fatalError("database init failed") } let dbFile = DatabaseFile( @@ -90,7 +90,7 @@ public class DatabaseFileManager { database = db } catch { - fatalError("Unprocessed exception while opening database. Possibly hardware failure has corrupted the data on this device.") + Logger.fatalError("Unprocessed exception while opening database. Possibly hardware failure has corrupted the data on this device.") } return dbFile } @@ -98,7 +98,7 @@ public class DatabaseFileManager { public func saveToFile(db: Database?) { do { guard let targetDatabase = db ?? database else { - fatalError("No database to save") + Logger.fatalError("No database to save") } let fileData = try targetDatabase.save() try fileData.write(to: kdbxAutofillURL, options: .atomic) diff --git a/ios/KdbxSwift/Sources/KdbxSwift/base32/Base32.swift b/ios/KdbxSwift/Sources/KdbxSwift/base32/Base32.swift index da05451..4aae4ff 100644 --- a/ios/KdbxSwift/Sources/KdbxSwift/base32/Base32.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/base32/Base32.swift @@ -25,6 +25,7 @@ // THE SOFTWARE. import Foundation +import os.log // https://tools.ietf.org/html/rfc4648 @@ -232,7 +233,7 @@ private func base32encode(_ data: UnsafeRawPointer, _ length: Int, _ table: [Int return base32Encoded } else { resultBuffer.deallocate() - fatalError("internal error") + Logger.fatalError("internal error") } } diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/Database.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/Database.swift index 91b5cda..ef5d0cd 100755 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/Database.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/Database.swift @@ -42,7 +42,7 @@ open class Database: Eraseable { } public var keyHelper: KeyHelper { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } internal init() { @@ -59,7 +59,7 @@ open class Database: Eraseable { } public class func isSignatureMatches(data: ByteArray) -> Bool { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func load( @@ -67,19 +67,19 @@ open class Database: Eraseable { dbFileData: ByteArray, preTransformedKeyMaterial: ByteArray ) throws { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func save() throws -> ByteArray { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func changeCompositeKey(to newKey: CompositeKey) { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func getBackupGroup(createIfMissing: Bool) -> Group? { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func count(includeGroups: Bool = true, includeEntries: Bool = true) -> Int { @@ -101,15 +101,15 @@ open class Database: Eraseable { } public func delete(group: Group) { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func delete(entry: Entry) { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func makeAttachment(name: String, data: ByteArray) -> Attachment { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } internal func resolveReferences( diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/DatabaseItem.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/DatabaseItem.swift index 79d220f..8173222 100644 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/DatabaseItem.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/DatabaseItem.swift @@ -1,3 +1,5 @@ +import os.log + open class DatabaseItem { public enum TouchMode { case accessed @@ -18,6 +20,6 @@ open class DatabaseItem { } public func touch(_ mode: TouchMode, updateParents: Bool = true) { - fatalError("Pure abstract method") + Logger.fatalError("Pure abstract method") } } diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/Entry.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/Entry.swift index 98fc9ec..7941e83 100644 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/Entry.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/Entry.swift @@ -1,4 +1,5 @@ import Foundation +import os.log public class EntryField: Eraseable { public static let title = "Title" @@ -203,7 +204,7 @@ public class Entry: DatabaseItem, Eraseable { public var isHiddenFromSearch: Bool { get { return false } - set { fatalError("This property can be modified only in some DB formats") } + set { Logger.fatalError("This property can be modified only in some DB formats") } } public var attachments: Array @@ -298,7 +299,7 @@ public class Entry: DatabaseItem, Eraseable { } public func clone(makeNewUUID: Bool) -> Entry { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func apply(to target: Entry, makeNewUUID: Bool) { @@ -326,7 +327,7 @@ public class Entry: DatabaseItem, Eraseable { } public func backupState() { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } override public func touch(_ mode: DatabaseItem.TouchMode, updateParents: Bool = true) { diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/Group.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/Group.swift index 0ff44dc..ebe4ae6 100755 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/Group.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/Group.swift @@ -1,4 +1,5 @@ import Foundation +import os.log public class Group: DatabaseItem, Eraseable { public static let defaultIconID = IconID.folder @@ -79,7 +80,7 @@ public class Group: DatabaseItem, Eraseable { } public func clone(makeNewUUID: Bool) -> Group { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func deepClone(makeNewUUIDs: Bool) -> Group { @@ -192,11 +193,11 @@ public class Group: DatabaseItem, Eraseable { } public func createEntry(detached: Bool = false) -> Entry { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func createGroup(detached: Bool = false) -> Group { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } override public func touch(_ mode: DatabaseItem.TouchMode, updateParents: Bool = true) { diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/KeyHelper.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/KeyHelper.swift index b435db5..b13dd7d 100755 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/KeyHelper.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/KeyHelper.swift @@ -9,15 +9,15 @@ public class KeyHelper { passwordData: ByteArray, keyFileData: ByteArray ) throws -> ByteArray { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func getKey(fromCombinedComponents combinedComponents: ByteArray) -> ByteArray { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func getPasswordData(password: String) -> ByteArray { - fatalError("Pure virtual method") + Logger.fatalError("Pure virtual method") } public func processKeyFile(keyFileData: ByteArray) throws -> ByteArray { diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/cipher/DataCipher.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/cipher/DataCipher.swift index 7d799da..33c14e8 100755 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/cipher/DataCipher.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/cipher/DataCipher.swift @@ -1,4 +1,5 @@ import Foundation +import os.log protocol DataCipher: AnyObject { var uuid: UUID { get } @@ -48,7 +49,7 @@ extension DataCipher { if keySize < hashSize { return hash.prefix(keySize) } else { - fatalError("Not implemented") + Logger.fatalError("Not implemented") } } } diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/kdf/Argon2KDF.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/kdf/Argon2KDF.swift index 49c85b5..e7711a9 100755 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/kdf/Argon2KDF.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/kdf/Argon2KDF.swift @@ -1,4 +1,5 @@ import Foundation +import os.log class AbstractArgon2KDF { public static let saltParam = "S" @@ -25,10 +26,10 @@ class AbstractArgon2KDF { fileprivate let defaultParallelism: UInt32 = 2 fileprivate var name: String { - fatalError("Abstract method, override this") + Logger.fatalError("Abstract method, override this") } fileprivate var uuid: UUID { - fatalError("Abstract method, override this") + Logger.fatalError("Abstract method, override this") } fileprivate var progress = ProgressEx() @@ -111,7 +112,7 @@ class AbstractArgon2KDF { } func transform(key: ByteArray, params: KDFParams) -> ByteArray { - fatalError("argon2 not implemented") + Logger.fatalError("argon2 not implemented") } } diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/kp2/Database2.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/kp2/Database2.swift index 1821430..a71e128 100644 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/kp2/Database2.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/kp2/Database2.swift @@ -591,7 +591,7 @@ public class Database2: Database { try header.writeInner(to: contentStream) Logger.mainLog.trace("Header written OK") contentStream.write(data: xmlData) - guard let contentData = contentStream.data else { fatalError() } + guard let contentData = contentStream.data else { Logger.fatalError("Failed to get data from contentStream") } var dataToEncrypt = contentData if header.isCompressed { @@ -720,7 +720,7 @@ public class Database2: Database { } override public func delete(group: Group) { - guard let group = group as? Group2 else { fatalError() } + guard let group = group as? Group2 else { Logger.fatalError("Cannot delete group: not a Group") } guard let parentGroup = group.parent else { Logger.mainLog.warning("Cannot delete group: no parent group") return diff --git a/ios/KdbxSwift/Sources/KdbxSwift/db/kp2/Header2.swift b/ios/KdbxSwift/Sources/KdbxSwift/db/kp2/Header2.swift index 02eac5d..7899539 100755 --- a/ios/KdbxSwift/Sources/KdbxSwift/db/kp2/Header2.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/db/kp2/Header2.swift @@ -449,7 +449,7 @@ final class Header2: Eraseable { internal func initStreamCipher() { guard let protectedStreamKey = protectedStreamKey else { - fatalError() + Logger.fatalError("initStreamCipher: Failed to assign protectedStreamKey") } self.streamCipher = StreamCipherFactory.create( algorithm: innerStreamAlgorithm, @@ -581,7 +581,7 @@ final class Header2: Eraseable { func writeInner(to stream: ByteArray.OutputStream) throws { assert(formatVersion >= .v4) - guard let protectedStreamKey = protectedStreamKey else { fatalError() } + guard let protectedStreamKey = protectedStreamKey else { Logger.fatalError("writeInner: failed to assign protectedStreamKey") } Logger.mainLog.trace("Writing kdbx4 inner header") stream.write(value: InnerFieldID.innerRandomStreamID.rawValue) diff --git a/ios/KdbxSwift/Sources/KdbxSwift/util/Extensions.swift b/ios/KdbxSwift/Sources/KdbxSwift/util/Extensions.swift index 491ed88..166ec06 100644 --- a/ios/KdbxSwift/Sources/KdbxSwift/util/Extensions.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/util/Extensions.swift @@ -1,4 +1,5 @@ import Foundation +import os.log extension StringProtocol { func base64ToBase64url() -> String { @@ -145,7 +146,7 @@ extension UUID { internal var data: ByteArray { var bytes = Array(repeating: 0, count: UUID.byteWidth) guard let nsuuid = NSUUID(uuidString: self.uuidString) else { - fatalError() + Logger.fatalError("failed to create NSUUID") } nsuuid.getBytes(&bytes) return ByteArray(bytes: bytes) diff --git a/ios/KdbxSwift/Sources/KdbxSwift/util/Logger.swift b/ios/KdbxSwift/Sources/KdbxSwift/util/Logger.swift index 6e69729..807789f 100755 --- a/ios/KdbxSwift/Sources/KdbxSwift/util/Logger.swift +++ b/ios/KdbxSwift/Sources/KdbxSwift/util/Logger.swift @@ -5,6 +5,11 @@ extension Logger { private static var subsystem = Bundle.main.bundleIdentifier! static let mainLog = Logger(subsystem: subsystem, category: "main") + + static func fatalError(_ message:String) -> Never { + mainLog.fault("\(message)") + Swift.fatalError(message) + } } //