| | | 1 | | using System.Text.Json; |
| | | 2 | | |
| | | 3 | | namespace LOCKnet.Core.DataAbstractions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Persistierter Vault-Header. |
| | | 7 | | /// Er enthaelt die Informationen, die benoetigt werden, um aus dem Master-Passwort |
| | | 8 | | /// einen KEK abzuleiten und damit den eigentlichen Vault-Key zu entpacken. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class VaultHeader |
| | | 11 | | { |
| | | 12 | | /// <summary>Aktuelle Versionsnummer des Header-Formats.</summary> |
| | 1691 | 13 | | public int FormatVersion { get; set; } = VaultHeaderFormatVersion.WrappedVaultKeyV1; |
| | | 14 | | |
| | | 15 | | /// <summary>Bezeichner der verwendeten KDF, z.B. <c>PBKDF2-SHA256</c>.</summary> |
| | 1320 | 16 | | public string KdfIdentifier { get; set; } = string.Empty; |
| | | 17 | | |
| | | 18 | | /// <summary>Persistierte Parameter fuer die KDF.</summary> |
| | 2003 | 19 | | public VaultKdfParameters KdfParameters { get; set; } = new(); |
| | | 20 | | |
| | | 21 | | /// <summary>Salt fuer die KEK-Ableitung.</summary> |
| | 1365 | 22 | | public byte[] Salt { get; set; } = []; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// AES-GCM-verpackter Vault-Key. |
| | | 26 | | /// Der Inhalt wird mit dem aus dem Master-Passwort abgeleiteten KEK entpackt. |
| | | 27 | | /// </summary> |
| | 1432 | 28 | | public byte[] WrappedVaultKey { get; set; } = []; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Kompatibilitaets-Hash fuer Legacy-Migrationen vorhandener Datenbanken. |
| | | 32 | | /// Neue Unlocks verlassen sich primaer auf <see cref="WrappedVaultKey"/>. |
| | | 33 | | /// </summary> |
| | 1336 | 34 | | public byte[] LegacyPasswordHash { get; set; } = []; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gibt an, ob der aktuell verpackte VaultKey noch aus einem Legacy-KDF-Schluessel stammt |
| | | 38 | | /// und deshalb vor kryptografisch abgeschlossener Passwortrotation ersetzt werden muss. |
| | | 39 | | /// </summary> |
| | 821 | 40 | | public bool UsesLegacyKeyMaterial { get; set; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Markiert, dass nach einer Migration oder Plausibilitaetsverletzung noch eine SQLite-Kompaktierung aussteht. |
| | | 44 | | /// </summary> |
| | 850 | 45 | | public bool RequiresStorageCompaction { get; set; } |
| | | 46 | | |
| | | 47 | | /// <summary>UTC-Zeitstempel des letzten Kompaktierungsversuchs.</summary> |
| | 745 | 48 | | public DateTime? LastStorageCompactionAttemptUtc { get; set; } |
| | | 49 | | |
| | | 50 | | /// <summary>Klassifizierter Fehler des letzten Kompaktierungsversuchs.</summary> |
| | 730 | 51 | | public StorageCompactionFailureKind LastStorageCompactionFailureKind { get; set; } |
| | | 52 | | |
| | | 53 | | /// <summary>Technische Kurzinfo des letzten Kompaktierungsversuchs.</summary> |
| | 717 | 54 | | public string? LastStorageCompactionError { get; set; } |
| | | 55 | | |
| | | 56 | | /// <summary>Persistierter Zustand einer spaeteren Plain-zu-encrypted-Storage-Migration.</summary> |
| | 396 | 57 | | public VaultStorageMigrationState StorageMigrationState { get; set; } |
| | | 58 | | |
| | | 59 | | /// <summary>Aktuelles Ziel der persistierten Storage-Migration.</summary> |
| | 369 | 60 | | public VaultStorageMigrationTargetMode StorageMigrationTargetMode { get; set; } |
| | | 61 | | |
| | | 62 | | /// <summary>UTC-Zeitstempel des letzten Storage-Migrationsversuchs.</summary> |
| | 369 | 63 | | public DateTime? LastStorageMigrationAttemptUtc { get; set; } |
| | | 64 | | |
| | | 65 | | /// <summary>Technische Kurzinfo des letzten Storage-Migrationsfehlers.</summary> |
| | 371 | 66 | | public string? LastStorageMigrationError { get; set; } |
| | | 67 | | |
| | | 68 | | /// <summary>UTC-Zeitstempel der Erstellung.</summary> |
| | 670 | 69 | | public DateTime CreatedAt { get; set; } |
| | | 70 | | |
| | | 71 | | /// <summary>UTC-Zeitstempel der letzten Aenderung.</summary> |
| | 712 | 72 | | public DateTime UpdatedAt { get; set; } |
| | | 73 | | |
| | | 74 | | /// <summary>Serialisiert den Header in JSON, z.B. fuer Tests oder Exportformate.</summary> |
| | | 75 | | public string Serialize() |
| | 8 | 76 | | => JsonSerializer.Serialize(this); |
| | | 77 | | |
| | | 78 | | /// <summary>Deserialisiert einen JSON-String in einen Vault-Header.</summary> |
| | | 79 | | /// <param name="json">Der serialisierte JSON-String.</param> |
| | | 80 | | public static VaultHeader Deserialize(string json) |
| | 1 | 81 | | { |
| | 1 | 82 | | ArgumentException.ThrowIfNullOrWhiteSpace(json); |
| | 1 | 83 | | return JsonSerializer.Deserialize<VaultHeader>(json) |
| | 1 | 84 | | ?? throw new InvalidOperationException("Vault-Header konnte nicht deserialisiert werden."); |
| | 1 | 85 | | } |
| | | 86 | | } |