| | | 1 | | using LOCKnet.Core.DataAbstractions; |
| | | 2 | | using LOCKnet.Data.Repositories; |
| | | 3 | | using Microsoft.Data.Sqlite; |
| | | 4 | | |
| | | 5 | | namespace LOCKnet.Data; |
| | | 6 | | |
| | | 7 | | internal sealed class SqlCipherEncryptedVaultMigrationExporter : IEncryptedVaultMigrationExporter |
| | | 8 | | { |
| | | 9 | | private readonly string _password; |
| | | 10 | | private readonly Func<SqlCipherRuntimeProbeResult>? _runtimeProbeOverride; |
| | | 11 | | |
| | | 12 | | internal SqlCipherEncryptedVaultMigrationExporter(string password) |
| | 12 | 13 | | : this(password, null) |
| | 12 | 14 | | { |
| | 12 | 15 | | } |
| | | 16 | | |
| | 15 | 17 | | internal SqlCipherEncryptedVaultMigrationExporter(string password, Func<SqlCipherRuntimeProbeResult>? runtimeProbeOver |
| | 15 | 18 | | { |
| | 15 | 19 | | ArgumentException.ThrowIfNullOrWhiteSpace(password); |
| | 15 | 20 | | _password = password; |
| | 15 | 21 | | _runtimeProbeOverride = runtimeProbeOverride; |
| | 15 | 22 | | } |
| | | 23 | | |
| | 3 | 24 | | public VaultStorageMigrationTargetMode TargetMode => VaultStorageMigrationTargetMode.EncryptedSqlite; |
| | | 25 | | |
| | | 26 | | internal SqlCipherRuntimeProbeResult ProbeRuntime() |
| | 34 | 27 | | { |
| | 34 | 28 | | if (_runtimeProbeOverride is not null) |
| | 3 | 29 | | return _runtimeProbeOverride(); |
| | | 30 | | |
| | 31 | 31 | | var providerPath = GetConfiguredProviderPath(); |
| | 31 | 32 | | var productionCrediblePath = providerPath is SqlCipherProviderPackagingPath.OfficialZetetic; |
| | | 33 | | |
| | | 34 | | try |
| | 31 | 35 | | { |
| | | 36 | | #if USE_OFFICIAL_SQLCIPHER_PATH |
| | | 37 | | if (providerPath is SqlCipherProviderPackagingPath.BundleZeteticWithProviderSqlcipher) |
| | | 38 | | SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlcipher()); |
| | | 39 | | #endif |
| | 31 | 40 | | } |
| | 0 | 41 | | catch (Exception ex) |
| | 0 | 42 | | { |
| | 0 | 43 | | return new SqlCipherRuntimeProbeResult( |
| | 0 | 44 | | false, |
| | 0 | 45 | | null, |
| | 0 | 46 | | null, |
| | 0 | 47 | | providerPath, |
| | 0 | 48 | | productionCrediblePath, |
| | 0 | 49 | | SqlCipherExporterFailureKind.NativeProviderLoadFailure, |
| | 0 | 50 | | $"SQLCipher provider activation failed: {GetInnermostMessage(ex)}"); |
| | | 51 | | } |
| | | 52 | | |
| | 31 | 53 | | SQLitePCL.Batteries_V2.Init(); |
| | | 54 | | |
| | | 55 | | try |
| | 31 | 56 | | { |
| | 31 | 57 | | using var connection = new SqliteConnection("Data Source=:memory:"); |
| | 31 | 58 | | connection.Open(); |
| | 31 | 59 | | using var versionCommand = connection.CreateCommand(); |
| | 31 | 60 | | versionCommand.CommandText = "PRAGMA cipher_version;"; |
| | 31 | 61 | | var cipherVersion = Convert.ToString(versionCommand.ExecuteScalar()) ?? string.Empty; |
| | 31 | 62 | | if (string.IsNullOrWhiteSpace(cipherVersion)) |
| | 0 | 63 | | return new SqlCipherRuntimeProbeResult(false, null, connection.ServerVersion, providerPath, productionCrediblePa |
| | | 64 | | |
| | 31 | 65 | | return new SqlCipherRuntimeProbeResult(true, cipherVersion, connection.ServerVersion, providerPath, productionCred |
| | | 66 | | } |
| | 0 | 67 | | catch (Exception ex) |
| | 0 | 68 | | { |
| | 0 | 69 | | var message = GetInnermostMessage(ex); |
| | 0 | 70 | | return new SqlCipherRuntimeProbeResult(false, null, null, providerPath, productionCrediblePath, SqlCipherExporterF |
| | | 71 | | } |
| | 34 | 72 | | } |
| | | 73 | | |
| | | 74 | | public void ExportPlaintextVault(string sourceConnectionString, string destinationPath) |
| | 10 | 75 | | { |
| | 10 | 76 | | ArgumentException.ThrowIfNullOrWhiteSpace(sourceConnectionString); |
| | 10 | 77 | | ArgumentException.ThrowIfNullOrWhiteSpace(destinationPath); |
| | | 78 | | |
| | 10 | 79 | | EnsureRuntimeAvailable(); |
| | | 80 | | |
| | 9 | 81 | | var sourcePath = StorageRewriteArtifacts.TryResolveDatabasePath(sourceConnectionString) |
| | 9 | 82 | | ?? throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.MigrationExportFailure, "SQLCi |
| | | 83 | | |
| | 9 | 84 | | if (File.Exists(destinationPath) && !StorageRewriteArtifacts.TryDeleteFile(destinationPath)) |
| | 0 | 85 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.MigrationExportFailure, "Vorhande |
| | | 86 | | |
| | | 87 | | try |
| | 9 | 88 | | { |
| | 9 | 89 | | var destinationFactory = new SqlCipherConnectionFactory(destinationPath, _password); |
| | 9 | 90 | | new Database(destinationFactory).Initialize(); |
| | | 91 | | |
| | 8 | 92 | | using var sourceConnection = new SqliteConnection(sourceConnectionString); |
| | 8 | 93 | | sourceConnection.Open(); |
| | 8 | 94 | | RepositoryBase.ConfigureConnection(sourceConnection); |
| | | 95 | | |
| | 8 | 96 | | using var destinationConnection = destinationFactory.OpenConnection(); |
| | 8 | 97 | | using var transaction = destinationConnection.BeginTransaction(); |
| | | 98 | | |
| | 8 | 99 | | CopyMasterKey(sourceConnection, destinationConnection, transaction); |
| | 8 | 100 | | CopyCredentials(sourceConnection, destinationConnection, transaction); |
| | 8 | 101 | | CopySettings(sourceConnection, destinationConnection, transaction); |
| | | 102 | | |
| | 8 | 103 | | transaction.Commit(); |
| | 8 | 104 | | } |
| | 0 | 105 | | catch (SqlCipherEncryptedVaultMigrationException) |
| | 0 | 106 | | { |
| | 0 | 107 | | throw; |
| | | 108 | | } |
| | 1 | 109 | | catch (Exception ex) when (ex is SqliteException or IOException or InvalidOperationException) |
| | 1 | 110 | | { |
| | 1 | 111 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.MigrationExportFailure, GetInnerm |
| | | 112 | | } |
| | 8 | 113 | | } |
| | | 114 | | |
| | | 115 | | public void ValidateExportedVault(string destinationPath) |
| | 7 | 116 | | { |
| | 7 | 117 | | ArgumentException.ThrowIfNullOrWhiteSpace(destinationPath); |
| | 7 | 118 | | EnsureRuntimeAvailable(); |
| | | 119 | | |
| | 7 | 120 | | if (!File.Exists(destinationPath)) |
| | 1 | 121 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.InvalidTarget, "SQLCipher-Zielart |
| | | 122 | | |
| | 6 | 123 | | var openResult = TryOpenEncryptedVault(destinationPath, _password); |
| | 6 | 124 | | if (!openResult.Success) |
| | 1 | 125 | | throw new SqlCipherEncryptedVaultMigrationException(openResult.FailureKind, openResult.Message ?? "SQLCipher-Ziela |
| | | 126 | | |
| | | 127 | | try |
| | 5 | 128 | | { |
| | 5 | 129 | | using var connection = OpenEncryptedConnection(destinationPath, _password, SqliteOpenMode.ReadOnly); |
| | 5 | 130 | | RequireCipherVersion(connection); |
| | 5 | 131 | | RequireQuickCheck(connection); |
| | 5 | 132 | | RequireTable(connection, "MasterKey"); |
| | 5 | 133 | | RequireTable(connection, "Credentials"); |
| | 5 | 134 | | RequireTable(connection, "Settings"); |
| | | 135 | | |
| | 4 | 136 | | using var masterKeyCount = connection.CreateCommand(); |
| | 4 | 137 | | masterKeyCount.CommandText = "SELECT COUNT(*) FROM MasterKey;"; |
| | 4 | 138 | | if (Convert.ToInt64(masterKeyCount.ExecuteScalar() ?? 0L) != 1) |
| | 1 | 139 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.ValidationFailure, "SQLCipher-Z |
| | 3 | 140 | | } |
| | 2 | 141 | | catch (SqlCipherEncryptedVaultMigrationException) |
| | 2 | 142 | | { |
| | 2 | 143 | | throw; |
| | | 144 | | } |
| | 0 | 145 | | catch (Exception ex) when (ex is SqliteException or IOException or InvalidOperationException) |
| | 0 | 146 | | { |
| | 0 | 147 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.OperationalFailure, GetInnermostM |
| | | 148 | | } |
| | 3 | 149 | | } |
| | | 150 | | |
| | | 151 | | public void PersistMigratedHeader(string databasePath, VaultHeader header) |
| | 4 | 152 | | { |
| | 4 | 153 | | ArgumentException.ThrowIfNullOrWhiteSpace(databasePath); |
| | 4 | 154 | | ArgumentNullException.ThrowIfNull(header); |
| | 4 | 155 | | EnsureRuntimeAvailable(); |
| | | 156 | | |
| | | 157 | | try |
| | 4 | 158 | | { |
| | 4 | 159 | | using var connection = OpenEncryptedConnection(databasePath, _password, SqliteOpenMode.ReadWrite); |
| | 4 | 160 | | using var command = connection.CreateCommand(); |
| | 4 | 161 | | command.CommandText = @" |
| | 4 | 162 | | UPDATE MasterKey |
| | 4 | 163 | | SET PasswordHash = $hash, |
| | 4 | 164 | | FormatVersion = $formatVersion, |
| | 4 | 165 | | KdfIdentifier = $kdfIdentifier, |
| | 4 | 166 | | KdfParameters = $kdfParameters, |
| | 4 | 167 | | Salt = $salt, |
| | 4 | 168 | | WrappedVaultKey = $wrappedVaultKey, |
| | 4 | 169 | | UsesLegacyKeyMaterial = $usesLegacyKeyMaterial, |
| | 4 | 170 | | RequiresStorageCompaction = $requiresStorageCompaction, |
| | 4 | 171 | | LastStorageCompactionAttemptUtc = $lastStorageCompactionAttemptUtc, |
| | 4 | 172 | | LastStorageCompactionFailureKind = $lastStorageCompactionFailureKind, |
| | 4 | 173 | | LastStorageCompactionError = $lastStorageCompactionError, |
| | 4 | 174 | | StorageMigrationState = $storageMigrationState, |
| | 4 | 175 | | StorageMigrationTargetMode = $storageMigrationTargetMode, |
| | 4 | 176 | | LastStorageMigrationAttemptUtc = $lastStorageMigrationAttemptUtc, |
| | 4 | 177 | | LastStorageMigrationError = $lastStorageMigrationError, |
| | 4 | 178 | | UpdatedAt = CURRENT_TIMESTAMP |
| | 4 | 179 | | WHERE Id = 1;"; |
| | 4 | 180 | | command.Parameters.AddWithValue("$hash", header.LegacyPasswordHash); |
| | 4 | 181 | | command.Parameters.AddWithValue("$formatVersion", header.FormatVersion); |
| | 4 | 182 | | command.Parameters.AddWithValue("$kdfIdentifier", header.KdfIdentifier); |
| | 4 | 183 | | command.Parameters.AddWithValue("$kdfParameters", header.KdfParameters.Serialize()); |
| | 4 | 184 | | command.Parameters.AddWithValue("$salt", header.Salt); |
| | 4 | 185 | | command.Parameters.AddWithValue("$wrappedVaultKey", (object?)header.WrappedVaultKey ?? DBNull.Value); |
| | 4 | 186 | | command.Parameters.AddWithValue("$usesLegacyKeyMaterial", header.UsesLegacyKeyMaterial ? 1 : 0); |
| | 4 | 187 | | command.Parameters.AddWithValue("$requiresStorageCompaction", header.RequiresStorageCompaction ? 1 : 0); |
| | 4 | 188 | | command.Parameters.AddWithValue("$lastStorageCompactionAttemptUtc", (object?)header.LastStorageCompactionAttemptUt |
| | 4 | 189 | | command.Parameters.AddWithValue("$lastStorageCompactionFailureKind", (int)header.LastStorageCompactionFailureKind) |
| | 4 | 190 | | command.Parameters.AddWithValue("$lastStorageCompactionError", (object?)header.LastStorageCompactionError ?? DBNul |
| | 4 | 191 | | command.Parameters.AddWithValue("$storageMigrationState", (int)header.StorageMigrationState); |
| | 4 | 192 | | command.Parameters.AddWithValue("$storageMigrationTargetMode", (int)header.StorageMigrationTargetMode); |
| | 4 | 193 | | command.Parameters.AddWithValue("$lastStorageMigrationAttemptUtc", (object?)header.LastStorageMigrationAttemptUtc? |
| | 4 | 194 | | command.Parameters.AddWithValue("$lastStorageMigrationError", (object?)header.LastStorageMigrationError ?? DBNull. |
| | 4 | 195 | | command.ExecuteNonQuery(); |
| | 4 | 196 | | } |
| | 0 | 197 | | catch (SqlCipherEncryptedVaultMigrationException) |
| | 0 | 198 | | { |
| | 0 | 199 | | throw; |
| | | 200 | | } |
| | 0 | 201 | | catch (Exception ex) when (ex is SqliteException or IOException or InvalidOperationException) |
| | 0 | 202 | | { |
| | 0 | 203 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.OperationalFailure, GetInnermostM |
| | | 204 | | } |
| | 4 | 205 | | } |
| | | 206 | | |
| | | 207 | | internal SqlCipherVaultOpenResult TryOpenEncryptedVault(string destinationPath, string password) |
| | 11 | 208 | | { |
| | 11 | 209 | | ArgumentException.ThrowIfNullOrWhiteSpace(destinationPath); |
| | 11 | 210 | | ArgumentException.ThrowIfNullOrWhiteSpace(password); |
| | | 211 | | |
| | 11 | 212 | | var runtime = ProbeRuntime(); |
| | 11 | 213 | | if (!runtime.IsAvailable) |
| | 1 | 214 | | return new SqlCipherVaultOpenResult(false, runtime.CipherVersion, runtime.ProviderPath, runtime.IsProductionCredib |
| | | 215 | | |
| | 10 | 216 | | if (!File.Exists(destinationPath)) |
| | 1 | 217 | | return new SqlCipherVaultOpenResult(false, runtime.CipherVersion, runtime.ProviderPath, runtime.IsProductionCredib |
| | | 218 | | |
| | | 219 | | try |
| | 9 | 220 | | { |
| | 9 | 221 | | using var connection = OpenEncryptedConnection(destinationPath, password, SqliteOpenMode.ReadOnly); |
| | 7 | 222 | | var cipherVersion = RequireCipherVersion(connection); |
| | 7 | 223 | | using var command = connection.CreateCommand(); |
| | 7 | 224 | | command.CommandText = "SELECT COUNT(*) FROM sqlite_master;"; |
| | 7 | 225 | | _ = Convert.ToInt64(command.ExecuteScalar() ?? 0L); |
| | 7 | 226 | | return new SqlCipherVaultOpenResult(true, cipherVersion, runtime.ProviderPath, runtime.IsProductionCrediblePath, S |
| | | 227 | | } |
| | 2 | 228 | | catch (Exception ex) when (ex is SqliteException or InvalidOperationException) |
| | 2 | 229 | | { |
| | 2 | 230 | | var kind = ClassifyOpenFailure(ex); |
| | 2 | 231 | | return new SqlCipherVaultOpenResult(false, runtime.CipherVersion, runtime.ProviderPath, runtime.IsProductionCredib |
| | | 232 | | } |
| | 11 | 233 | | } |
| | | 234 | | |
| | | 235 | | private void EnsureRuntimeAvailable() |
| | 21 | 236 | | { |
| | 21 | 237 | | var runtime = ProbeRuntime(); |
| | 21 | 238 | | if (!runtime.IsAvailable) |
| | 1 | 239 | | throw new SqlCipherEncryptedVaultMigrationException(runtime.FailureKind, runtime.Message ?? "SQLCipher-Laufzeit ko |
| | 20 | 240 | | } |
| | | 241 | | |
| | | 242 | | private static SqliteConnection OpenEncryptedConnection(string databasePath, string password, SqliteOpenMode mode) |
| | 35 | 243 | | { |
| | 35 | 244 | | var builder = new SqliteConnectionStringBuilder |
| | 35 | 245 | | { |
| | 35 | 246 | | DataSource = databasePath, |
| | 35 | 247 | | Mode = mode, |
| | 35 | 248 | | Password = password, |
| | 35 | 249 | | }; |
| | | 250 | | |
| | 35 | 251 | | var connection = new SqliteConnection(builder.ToString()); |
| | 35 | 252 | | connection.Open(); |
| | 32 | 253 | | if (mode != SqliteOpenMode.ReadOnly) |
| | 20 | 254 | | RepositoryBase.ConfigureConnection(connection); |
| | 32 | 255 | | return connection; |
| | 32 | 256 | | } |
| | | 257 | | |
| | | 258 | | private static string RequireCipherVersion(SqliteConnection connection) |
| | 12 | 259 | | { |
| | 12 | 260 | | using var command = connection.CreateCommand(); |
| | 12 | 261 | | command.CommandText = "PRAGMA cipher_version;"; |
| | 12 | 262 | | var cipherVersion = Convert.ToString(command.ExecuteScalar()) ?? string.Empty; |
| | 12 | 263 | | if (string.IsNullOrWhiteSpace(cipherVersion)) |
| | 0 | 264 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.NativeProviderLoadFailure, "PRAGM |
| | | 265 | | |
| | 12 | 266 | | return cipherVersion; |
| | 12 | 267 | | } |
| | | 268 | | |
| | | 269 | | private static void RequireQuickCheck(SqliteConnection connection) |
| | 5 | 270 | | { |
| | 5 | 271 | | using var command = connection.CreateCommand(); |
| | 5 | 272 | | command.CommandText = "PRAGMA quick_check(1);"; |
| | 5 | 273 | | var result = Convert.ToString(command.ExecuteScalar()) ?? string.Empty; |
| | 5 | 274 | | if (!string.Equals(result, "ok", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 275 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.ValidationFailure, "SQLCipher qui |
| | 10 | 276 | | } |
| | | 277 | | |
| | | 278 | | private static void RequireTable(SqliteConnection connection, string tableName) |
| | 15 | 279 | | { |
| | 15 | 280 | | using var command = connection.CreateCommand(); |
| | 15 | 281 | | command.CommandText = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=$name;"; |
| | 15 | 282 | | command.Parameters.AddWithValue("$name", tableName); |
| | 15 | 283 | | if (Convert.ToInt64(command.ExecuteScalar() ?? 0L) <= 0) |
| | 1 | 284 | | throw new SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind.ValidationFailure, $"SQLCipher-Zi |
| | 28 | 285 | | } |
| | | 286 | | |
| | | 287 | | private static void CopyMasterKey(SqliteConnection sourceConnection, SqliteConnection destinationConnection, SqliteTra |
| | 8 | 288 | | { |
| | 8 | 289 | | using var sourceCommand = sourceConnection.CreateCommand(); |
| | 8 | 290 | | sourceCommand.CommandText = @" |
| | 8 | 291 | | SELECT Id, PasswordHash, FormatVersion, KdfIdentifier, KdfParameters, Salt, WrappedVaultKey, UsesLegacyKeyMaterial |
| | 8 | 292 | | RequiresStorageCompaction, LastStorageCompactionAttemptUtc, LastStorageCompactionFailureKind, LastStorageCo |
| | 8 | 293 | | StorageMigrationState, StorageMigrationTargetMode, LastStorageMigrationAttemptUtc, LastStorageMigrationErro |
| | 8 | 294 | | FROM MasterKey;"; |
| | 8 | 295 | | using var reader = sourceCommand.ExecuteReader(); |
| | 16 | 296 | | while (reader.Read()) |
| | 8 | 297 | | { |
| | 8 | 298 | | using var insert = destinationConnection.CreateCommand(); |
| | 8 | 299 | | insert.Transaction = transaction; |
| | 8 | 300 | | insert.CommandText = @" |
| | 8 | 301 | | INSERT INTO MasterKey (Id, PasswordHash, FormatVersion, KdfIdentifier, KdfParameters, Salt, WrappedVaultKey, Use |
| | 8 | 302 | | RequiresStorageCompaction, LastStorageCompactionAttemptUtc, LastStorageCompactionFailureKind, LastStorageCompa |
| | 8 | 303 | | StorageMigrationState, StorageMigrationTargetMode, LastStorageMigrationAttemptUtc, LastStorageMigrationError, |
| | 8 | 304 | | VALUES ($id, $passwordHash, $formatVersion, $kdfIdentifier, $kdfParameters, $salt, $wrappedVaultKey, $usesLegacy |
| | 8 | 305 | | $requiresStorageCompaction, $lastStorageCompactionAttemptUtc, $lastStorageCompactionFailureKind, $lastStorageC |
| | 8 | 306 | | $storageMigrationState, $storageMigrationTargetMode, $lastStorageMigrationAttemptUtc, $lastStorageMigrationErr |
| | 8 | 307 | | insert.Parameters.AddWithValue("$id", reader.GetInt64(0)); |
| | 8 | 308 | | insert.Parameters.AddWithValue("$passwordHash", reader.IsDBNull(1) ? [] : (byte[])reader[1]); |
| | 8 | 309 | | insert.Parameters.AddWithValue("$formatVersion", reader.GetInt32(2)); |
| | 8 | 310 | | insert.Parameters.AddWithValue("$kdfIdentifier", reader.GetString(3)); |
| | 8 | 311 | | insert.Parameters.AddWithValue("$kdfParameters", reader.GetString(4)); |
| | 8 | 312 | | insert.Parameters.AddWithValue("$salt", (byte[])reader[5]); |
| | 8 | 313 | | insert.Parameters.AddWithValue("$wrappedVaultKey", reader.IsDBNull(6) ? DBNull.Value : reader[6]); |
| | 8 | 314 | | insert.Parameters.AddWithValue("$usesLegacyKeyMaterial", reader.GetInt32(7)); |
| | 8 | 315 | | insert.Parameters.AddWithValue("$requiresStorageCompaction", reader.GetInt32(8)); |
| | 8 | 316 | | insert.Parameters.AddWithValue("$lastStorageCompactionAttemptUtc", reader.IsDBNull(9) ? DBNull.Value : reader.GetS |
| | 8 | 317 | | insert.Parameters.AddWithValue("$lastStorageCompactionFailureKind", reader.GetInt32(10)); |
| | 8 | 318 | | insert.Parameters.AddWithValue("$lastStorageCompactionError", reader.IsDBNull(11) ? DBNull.Value : reader.GetStrin |
| | 8 | 319 | | insert.Parameters.AddWithValue("$storageMigrationState", reader.GetInt32(12)); |
| | 8 | 320 | | insert.Parameters.AddWithValue("$storageMigrationTargetMode", reader.GetInt32(13)); |
| | 8 | 321 | | insert.Parameters.AddWithValue("$lastStorageMigrationAttemptUtc", reader.IsDBNull(14) ? DBNull.Value : reader.GetS |
| | 8 | 322 | | insert.Parameters.AddWithValue("$lastStorageMigrationError", reader.IsDBNull(15) ? DBNull.Value : reader.GetString |
| | 8 | 323 | | insert.Parameters.AddWithValue("$createdAt", reader.GetDateTime(16)); |
| | 8 | 324 | | insert.Parameters.AddWithValue("$updatedAt", reader.GetDateTime(17)); |
| | 8 | 325 | | insert.ExecuteNonQuery(); |
| | 8 | 326 | | } |
| | 16 | 327 | | } |
| | | 328 | | |
| | | 329 | | private static void CopyCredentials(SqliteConnection sourceConnection, SqliteConnection destinationConnection, SqliteT |
| | 8 | 330 | | { |
| | 8 | 331 | | using var sourceCommand = sourceConnection.CreateCommand(); |
| | 8 | 332 | | sourceCommand.CommandText = @" |
| | 8 | 333 | | SELECT Id, Title, Username, EncryptedPassword, EncryptedMetadata, CredentialUuid, SecretFormatVersion, MetadataFor |
| | 8 | 334 | | URL, Notes, CreatedAt, UpdatedAt, IconKey, CredentialType |
| | 8 | 335 | | FROM Credentials;"; |
| | 8 | 336 | | using var reader = sourceCommand.ExecuteReader(); |
| | 16 | 337 | | while (reader.Read()) |
| | 8 | 338 | | { |
| | 8 | 339 | | using var insert = destinationConnection.CreateCommand(); |
| | 8 | 340 | | insert.Transaction = transaction; |
| | 8 | 341 | | insert.CommandText = @" |
| | 8 | 342 | | INSERT INTO Credentials (Id, Title, Username, EncryptedPassword, EncryptedMetadata, CredentialUuid, SecretFormat |
| | 8 | 343 | | URL, Notes, CreatedAt, UpdatedAt, IconKey, CredentialType) |
| | 8 | 344 | | VALUES ($id, $title, $username, $encryptedPassword, $encryptedMetadata, $credentialUuid, $secretFormatVersion, $ |
| | 8 | 345 | | $url, $notes, $createdAt, $updatedAt, $iconKey, $credentialType);"; |
| | 8 | 346 | | insert.Parameters.AddWithValue("$id", reader.GetInt64(0)); |
| | 8 | 347 | | insert.Parameters.AddWithValue("$title", reader.GetString(1)); |
| | 8 | 348 | | insert.Parameters.AddWithValue("$username", reader.IsDBNull(2) ? DBNull.Value : reader.GetString(2)); |
| | 8 | 349 | | insert.Parameters.AddWithValue("$encryptedPassword", (byte[])reader[3]); |
| | 8 | 350 | | insert.Parameters.AddWithValue("$encryptedMetadata", reader.IsDBNull(4) ? DBNull.Value : reader[4]); |
| | 8 | 351 | | insert.Parameters.AddWithValue("$credentialUuid", reader.GetString(5)); |
| | 8 | 352 | | insert.Parameters.AddWithValue("$secretFormatVersion", reader.GetInt32(6)); |
| | 8 | 353 | | insert.Parameters.AddWithValue("$metadataFormatVersion", reader.GetInt32(7)); |
| | 8 | 354 | | insert.Parameters.AddWithValue("$url", reader.IsDBNull(8) ? DBNull.Value : reader.GetString(8)); |
| | 8 | 355 | | insert.Parameters.AddWithValue("$notes", reader.IsDBNull(9) ? DBNull.Value : reader.GetString(9)); |
| | 8 | 356 | | insert.Parameters.AddWithValue("$createdAt", reader.GetDateTime(10)); |
| | 8 | 357 | | insert.Parameters.AddWithValue("$updatedAt", reader.GetDateTime(11)); |
| | 8 | 358 | | insert.Parameters.AddWithValue("$iconKey", reader.IsDBNull(12) ? DBNull.Value : reader.GetString(12)); |
| | 8 | 359 | | insert.Parameters.AddWithValue("$credentialType", reader.GetInt32(13)); |
| | 8 | 360 | | insert.ExecuteNonQuery(); |
| | 8 | 361 | | } |
| | 16 | 362 | | } |
| | | 363 | | |
| | | 364 | | private static void CopySettings(SqliteConnection sourceConnection, SqliteConnection destinationConnection, SqliteTran |
| | 8 | 365 | | { |
| | 8 | 366 | | using var sourceCommand = sourceConnection.CreateCommand(); |
| | 8 | 367 | | sourceCommand.CommandText = "SELECT Id, Key, Value, CreatedAt, UpdatedAt FROM Settings;"; |
| | 8 | 368 | | using var reader = sourceCommand.ExecuteReader(); |
| | 16 | 369 | | while (reader.Read()) |
| | 8 | 370 | | { |
| | 8 | 371 | | using var insert = destinationConnection.CreateCommand(); |
| | 8 | 372 | | insert.Transaction = transaction; |
| | 8 | 373 | | insert.CommandText = @" |
| | 8 | 374 | | INSERT INTO Settings (Id, Key, Value, CreatedAt, UpdatedAt) |
| | 8 | 375 | | VALUES ($id, $key, $value, $createdAt, $updatedAt);"; |
| | 8 | 376 | | insert.Parameters.AddWithValue("$id", reader.GetInt64(0)); |
| | 8 | 377 | | insert.Parameters.AddWithValue("$key", reader.GetString(1)); |
| | 8 | 378 | | insert.Parameters.AddWithValue("$value", reader.GetString(2)); |
| | 8 | 379 | | insert.Parameters.AddWithValue("$createdAt", reader.GetDateTime(3)); |
| | 8 | 380 | | insert.Parameters.AddWithValue("$updatedAt", reader.GetDateTime(4)); |
| | 8 | 381 | | insert.ExecuteNonQuery(); |
| | 8 | 382 | | } |
| | 16 | 383 | | } |
| | | 384 | | |
| | | 385 | | private static SqlCipherExporterFailureKind ClassifyOpenFailure(Exception exception) |
| | 7 | 386 | | { |
| | 7 | 387 | | if (exception is SqliteException sqliteException) |
| | 5 | 388 | | { |
| | 5 | 389 | | if (sqliteException.SqliteErrorCode == 26) |
| | 3 | 390 | | return SqlCipherExporterFailureKind.WrongKey; |
| | | 391 | | |
| | 2 | 392 | | if (sqliteException.Message.Contains("file is not a database", StringComparison.OrdinalIgnoreCase) || |
| | 2 | 393 | | sqliteException.Message.Contains("not a database", StringComparison.OrdinalIgnoreCase)) |
| | 1 | 394 | | { |
| | 1 | 395 | | return SqlCipherExporterFailureKind.WrongKey; |
| | | 396 | | } |
| | | 397 | | |
| | 1 | 398 | | return SqlCipherExporterFailureKind.InvalidTarget; |
| | | 399 | | } |
| | | 400 | | |
| | 2 | 401 | | if (exception.InnerException is not null) |
| | 1 | 402 | | return ClassifyOpenFailure(exception.InnerException); |
| | | 403 | | |
| | 1 | 404 | | return SqlCipherExporterFailureKind.OperationalFailure; |
| | 7 | 405 | | } |
| | | 406 | | |
| | | 407 | | private static string GetInnermostMessage(Exception exception) |
| | 3 | 408 | | { |
| | 3 | 409 | | var current = exception; |
| | 3 | 410 | | while (current.InnerException is not null) |
| | 0 | 411 | | current = current.InnerException; |
| | | 412 | | |
| | 3 | 413 | | return current.Message; |
| | 3 | 414 | | } |
| | | 415 | | |
| | | 416 | | private static SqlCipherProviderPackagingPath GetConfiguredProviderPath() |
| | 31 | 417 | | { |
| | | 418 | | #if USE_OFFICIAL_SQLCIPHER_PATH |
| | | 419 | | return SqlCipherProviderPackagingPath.BundleZeteticWithProviderSqlcipher; |
| | | 420 | | #else |
| | 31 | 421 | | return SqlCipherProviderPackagingPath.LegacyBundleESqlCipher; |
| | | 422 | | #endif |
| | 31 | 423 | | } |
| | | 424 | | |
| | | 425 | | private sealed class SqlCipherConnectionFactory : ISqliteConnectionFactory |
| | | 426 | | { |
| | | 427 | | private readonly string _databasePath; |
| | | 428 | | private readonly string _password; |
| | | 429 | | |
| | 9 | 430 | | public SqlCipherConnectionFactory(string databasePath, string password) |
| | 9 | 431 | | { |
| | 9 | 432 | | _databasePath = Path.GetFullPath(databasePath); |
| | 9 | 433 | | _password = password; |
| | 9 | 434 | | Storage = new VaultStorageDescriptor(VaultStorageMode.EncryptedSqlite, $"Data Source={_databasePath}", _databasePa |
| | 9 | 435 | | } |
| | | 436 | | |
| | 9 | 437 | | public VaultStorageDescriptor Storage { get; } |
| | | 438 | | |
| | | 439 | | public SqliteConnection OpenConnection() |
| | 17 | 440 | | => OpenEncryptedConnection(_databasePath, _password, SqliteOpenMode.ReadWriteCreate); |
| | | 441 | | } |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | internal enum SqlCipherExporterFailureKind |
| | | 445 | | { |
| | | 446 | | None = 0, |
| | | 447 | | NativeProviderLoadFailure = 1, |
| | | 448 | | CipherSupportUnavailable = 2, |
| | | 449 | | WrongKey = 3, |
| | | 450 | | InvalidTarget = 4, |
| | | 451 | | MigrationExportFailure = 5, |
| | | 452 | | ValidationFailure = 6, |
| | | 453 | | OperationalFailure = 7, |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | internal enum SqlCipherProviderPackagingPath |
| | | 457 | | { |
| | | 458 | | LegacyBundleESqlCipher = 0, |
| | | 459 | | BundleZeteticWithProviderSqlcipher = 1, |
| | | 460 | | OfficialZetetic = 2, |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | internal sealed class SqlCipherEncryptedVaultMigrationException : InvalidOperationException |
| | | 464 | | { |
| | | 465 | | internal SqlCipherEncryptedVaultMigrationException(SqlCipherExporterFailureKind failureKind, string message, Exception |
| | | 466 | | : base(message, innerException) |
| | | 467 | | { |
| | | 468 | | FailureKind = failureKind; |
| | | 469 | | } |
| | | 470 | | |
| | | 471 | | internal SqlCipherExporterFailureKind FailureKind { get; } |
| | | 472 | | } |
| | | 473 | | |
| | | 474 | | internal sealed record SqlCipherRuntimeProbeResult( |
| | | 475 | | bool IsAvailable, |
| | | 476 | | string? CipherVersion, |
| | | 477 | | string? SqliteVersion, |
| | | 478 | | SqlCipherProviderPackagingPath ProviderPath, |
| | | 479 | | bool IsProductionCrediblePath, |
| | | 480 | | SqlCipherExporterFailureKind FailureKind, |
| | | 481 | | string? Message); |
| | | 482 | | |
| | | 483 | | internal sealed record SqlCipherVaultOpenResult( |
| | | 484 | | bool Success, |
| | | 485 | | string? CipherVersion, |
| | | 486 | | SqlCipherProviderPackagingPath ProviderPath, |
| | | 487 | | bool IsProductionCrediblePath, |
| | | 488 | | SqlCipherExporterFailureKind FailureKind, |
| | | 489 | | string? Message); |