diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 478db52f5..c605062a9 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -616,8 +616,6 @@ 56AFCAD01CB1ABC800F48B96 /* GRDBTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */; }; 56AFCAD21CB1ABC800F48B96 /* GRDBCipher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 56AFCA231CB1A8BB00F48B96 /* GRDBCipher.framework */; }; 56AFCAD91CB1AC5C00F48B96 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; }; - 56AFCADA1CB1AD4E00F48B96 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; }; - 56AFCADB1CB1AD5100F48B96 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; }; 56B7F42A1BE14A1900E39BBF /* CGFloatTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B7F4291BE14A1900E39BBF /* CGFloatTests.swift */; }; 56B7F42F1BE14CD700E39BBF /* CGFloatTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B7F4291BE14A1900E39BBF /* CGFloatTests.swift */; }; 56B7F4371BE4C11200E39BBF /* DatabaseCoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B7F4361BE4C11200E39BBF /* DatabaseCoderTests.swift */; }; @@ -2206,7 +2204,6 @@ 560FC56A1CB00B880014AA8E /* SQLSupportTests.swift in Sources */, 560FC56B1CB00B880014AA8E /* CollationTests.swift in Sources */, 560FC56C1CB00B880014AA8E /* UpdateStatementTests.swift in Sources */, - 56AFCADB1CB1AD5100F48B96 /* EncryptionTests.swift in Sources */, 560FC56D1CB00B880014AA8E /* DatabaseMigratorTests.swift in Sources */, 560FC56E1CB00B880014AA8E /* DatabasePoolPersistableTests.swift in Sources */, 560FC56F1CB00B880014AA8E /* RecordAwakeFromFetchTests.swift in Sources */, @@ -2433,7 +2430,6 @@ 56AFCA3A1CB1AA9900F48B96 /* UpdateStatementTests.swift in Sources */, 56AFCA3B1CB1AA9900F48B96 /* DatabaseMigratorTests.swift in Sources */, 56AFCA3C1CB1AA9900F48B96 /* DatabasePoolPersistableTests.swift in Sources */, - 56AFCADA1CB1AD4E00F48B96 /* EncryptionTests.swift in Sources */, 56AFCA3D1CB1AA9900F48B96 /* RecordAwakeFromFetchTests.swift in Sources */, 56AFCA3E1CB1AA9900F48B96 /* DatabasePoolCollationTests.swift in Sources */, 56AFCA3F1CB1AA9900F48B96 /* PrimaryKeySingleTests.swift in Sources */, diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 554e7856a..1685931e4 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -887,6 +887,14 @@ extension Database { throw DatabaseError(code: code, message: String.fromCString(sqlite3_errmsg(sqliteConnection))) } } + + func changePassphrase(passphrase: String) throws { + let data = passphrase.dataUsingEncoding(NSUTF8StringEncoding)! + let code = sqlite3_rekey(sqliteConnection, data.bytes, Int32(data.length)) + guard code == SQLITE_OK else { + throw DatabaseError(code: code, message: String.fromCString(sqlite3_errmsg(sqliteConnection))) + } + } } #endif diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index c5479ebb8..654a3692f 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -56,7 +56,7 @@ public final class DatabasePool { } // Readers - var readerConfig = configuration + readerConfig = configuration readerConfig.readonly = true readerConfig.defaultTransactionKind = .Deferred // Make it the default. Other transaction kinds are forbidden by SQLite in read-only connections. @@ -64,7 +64,7 @@ public final class DatabasePool { readerPool.makeElement = { [unowned self] in let serializedDatabase = try! SerializedDatabase( path: path, - configuration: readerConfig, + configuration: self.readerConfig, schemaCache: databaseSchemaCache) serializedDatabase.performSync { db in @@ -192,6 +192,7 @@ public final class DatabasePool { let store: DatabaseStore // Not private for tests that require syncing with the store private let writer: SerializedDatabase + private var readerConfig: Configuration private let readerPool: Pool private var functions = Set() @@ -207,6 +208,23 @@ public enum CheckpointMode: Int32 { } +// ========================================================================= +// MARK: - Encryption + +#if SQLITE_HAS_CODEC + extension DatabasePool { + public func changePassphrase(passphrase: String) throws { + try readerPool.clear { + try self.writer.performSync { db in + try db.changePassphrase(passphrase) + } + self.readerConfig.passphrase = passphrase + } + } + } +#endif + + // ========================================================================= // MARK: - DatabaseReader diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index fa3befc06..57ebdb4d2 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -199,6 +199,20 @@ public final class DatabaseQueue { } +// ========================================================================= +// MARK: - Encryption + +#if SQLITE_HAS_CODEC + extension DatabaseQueue { + public func changePassphrase(passphrase: String) throws { + try serializedDatabase.performSync { db in + try db.changePassphrase(passphrase) + } + } + } +#endif + + // ========================================================================= // MARK: - DatabaseReader diff --git a/GRDB/Core/Utils.swift b/GRDB/Core/Utils.swift index 287b13242..83a6e86d1 100644 --- a/GRDB/Core/Utils.swift +++ b/GRDB/Core/Utils.swift @@ -221,11 +221,16 @@ final class Pool { } /// Empty the pool. Currently used items won't be reused. - /// Eventual block is executed before any other thread can load another element. - func clear(block: (() -> ())? = nil) { - dispatch_sync(queue) { + func clear() { + clear {} + } + + /// Empty the pool. Currently used items won't be reused. + /// Eventual block is executed before any other element is dequeued. + func clear(block: () throws -> ()) rethrows { + try dispatchSync(queue) { self.items = [] - block?() + try block() } } diff --git a/Tests/Public/Encryption/EncryptionTests.swift b/Tests/Public/Encryption/EncryptionTests.swift index 9ee03c955..f7589ede0 100644 --- a/Tests/Public/Encryption/EncryptionTests.swift +++ b/Tests/Public/Encryption/EncryptionTests.swift @@ -15,7 +15,7 @@ class EncryptionTests: GRDBTestCase { do { dbConfiguration.passphrase = "secret" let dbQueue = try makeDatabaseQueue() - XCTAssertEqual(Int.fetchOne(dbQueue, "SELECT value FROM data")!, 1) + XCTAssertEqual(Int.fetchOne(dbQueue, "SELECT COUNT(*) FROM data")!, 1) } } } @@ -67,6 +67,32 @@ class EncryptionTests: GRDBTestCase { } } + func testDatabaseQueueWithPassphraseToDatabaseQueueWithNewPassphrase() { + assertNoError { + + do { + dbConfiguration.passphrase = "secret" + let dbQueue = try makeDatabaseQueue() + try dbQueue.execute("CREATE TABLE data (value INTEGER)") + try dbQueue.execute("INSERT INTO data (value) VALUES (1)") + } + + do { + dbConfiguration.passphrase = "secret" + let dbQueue = try makeDatabaseQueue() + try dbQueue.changePassphrase("newSecret") + try dbQueue.execute("INSERT INTO data (value) VALUES (2)") + XCTAssertEqual(Int.fetchOne(dbQueue, "SELECT COUNT(*) FROM data")!, 2) + } + + do { + dbConfiguration.passphrase = "newSecret" + let dbQueue = try makeDatabaseQueue() + XCTAssertEqual(Int.fetchOne(dbQueue, "SELECT COUNT(*) FROM data")!, 2) + } + } + } + func testDatabaseQueueWithPassphraseToDatabasePoolWithPassphrase() { assertNoError { do { @@ -79,7 +105,7 @@ class EncryptionTests: GRDBTestCase { do { dbConfiguration.passphrase = "secret" let dbPool = try makeDatabasePool() - XCTAssertEqual(Int.fetchOne(dbPool, "SELECT value FROM data")!, 1) + XCTAssertEqual(Int.fetchOne(dbPool, "SELECT COUNT(*) FROM data")!, 1) } } } @@ -130,6 +156,31 @@ class EncryptionTests: GRDBTestCase { } } + func testDatabaseQueueWithPassphraseToDatabasePoolWithNewPassphrase() { + assertNoError { + do { + dbConfiguration.passphrase = "secret" + let dbQueue = try makeDatabaseQueue() + try dbQueue.execute("CREATE TABLE data (value INTEGER)") + try dbQueue.execute("INSERT INTO data (value) VALUES (1)") + } + + do { + dbConfiguration.passphrase = "secret" + let dbPool = try makeDatabasePool() + try dbPool.changePassphrase("newSecret") + try dbPool.execute("INSERT INTO data (value) VALUES (2)") + XCTAssertEqual(Int.fetchOne(dbPool, "SELECT COUNT(*) FROM data")!, 2) + } + + do { + dbConfiguration.passphrase = "newSecret" + let dbPool = try makeDatabasePool() + XCTAssertEqual(Int.fetchOne(dbPool, "SELECT COUNT(*) FROM data")!, 2) + } + } + } + func testDatabasePoolWithPassphraseToDatabasePoolWithPassphrase() { assertNoError { do { @@ -142,7 +193,7 @@ class EncryptionTests: GRDBTestCase { do { dbConfiguration.passphrase = "secret" let dbPool = try makeDatabasePool() - XCTAssertEqual(Int.fetchOne(dbPool, "SELECT value FROM data")!, 1) + XCTAssertEqual(Int.fetchOne(dbPool, "SELECT COUNT(*) FROM data")!, 1) } } } @@ -193,6 +244,32 @@ class EncryptionTests: GRDBTestCase { } } + func testDatabasePoolWithPassphraseToDatabasePoolWithNewPassphrase() { + assertNoError { + + do { + dbConfiguration.passphrase = "secret" + let dbPool = try makeDatabasePool() + try dbPool.execute("CREATE TABLE data (value INTEGER)") + try dbPool.execute("INSERT INTO data (value) VALUES (1)") + } + + do { + dbConfiguration.passphrase = "secret" + let dbPool = try makeDatabasePool() + try dbPool.changePassphrase("newSecret") + try dbPool.execute("INSERT INTO data (value) VALUES (2)") + XCTAssertEqual(Int.fetchOne(dbPool, "SELECT COUNT(*) FROM data")!, 2) + } + + do { + dbConfiguration.passphrase = "newSecret" + let dbPool = try makeDatabasePool() + XCTAssertEqual(Int.fetchOne(dbPool, "SELECT COUNT(*) FROM data")!, 2) + } + } + } + func testDatabaseQueueWithPragmaPassphraseToDatabaseQueueWithPassphrase() { assertNoError { do { @@ -206,7 +283,7 @@ class EncryptionTests: GRDBTestCase { do { dbConfiguration.passphrase = "secret" let dbQueue = try makeDatabaseQueue() - XCTAssertEqual(Int.fetchOne(dbQueue, "SELECT value FROM data")!, 1) + XCTAssertEqual(Int.fetchOne(dbQueue, "SELECT COUNT(*) FROM data")!, 1) } } }