| | | 1 | | using CommunityToolkit.Mvvm.ComponentModel; |
| | | 2 | | using LOCKnet.App.ViewModels; |
| | | 3 | | using LOCKnet.Core.Crypto; |
| | | 4 | | using System.Security; |
| | | 5 | | |
| | | 6 | | namespace LOCKnet.App.ViewModels; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// ViewModel für den Login-Screen (Ersteinrichtung + Entsperren). |
| | | 10 | | /// </summary> |
| | | 11 | | public partial class LoginViewModel : ViewModelBase |
| | | 12 | | { |
| | | 13 | | public event EventHandler? UnlockSucceeded; |
| | | 14 | | |
| | | 15 | | [ObservableProperty] |
| | 9 | 16 | | private string _errorMessage = string.Empty; |
| | | 17 | | |
| | | 18 | | [ObservableProperty] |
| | | 19 | | private bool _isSetupMode; |
| | | 20 | | |
| | 9 | 21 | | public LoginViewModel() |
| | 9 | 22 | | { |
| | 9 | 23 | | IsSetupMode = !AppServices.Current.MasterKeyManager.IsInitialized; |
| | 9 | 24 | | } |
| | | 25 | | |
| | | 26 | | public void Unlock(SecureString password) |
| | 4 | 27 | | { |
| | 4 | 28 | | ArgumentNullException.ThrowIfNull(password); |
| | 4 | 29 | | ErrorMessage = string.Empty; |
| | | 30 | | try |
| | 4 | 31 | | { |
| | 4 | 32 | | var unlock = AppServices.Current.MasterKeyManager.Unlock(password); |
| | 3 | 33 | | if (unlock is null) |
| | 2 | 34 | | { |
| | 2 | 35 | | ErrorMessage = "Falsches Passwort."; |
| | 2 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | AppServices.Current.SessionManager.Open(unlock.VaultKey); |
| | 1 | 40 | | AppServices.Current.ActivityMonitor.Start(); |
| | 1 | 41 | | UnlockSucceeded?.Invoke(this, EventArgs.Empty); |
| | 1 | 42 | | } |
| | 1 | 43 | | catch (Exception ex) |
| | 1 | 44 | | { |
| | 1 | 45 | | ErrorMessage = MapError(ex); |
| | 1 | 46 | | } |
| | 4 | 47 | | } |
| | | 48 | | |
| | | 49 | | public void Setup(SecureString password, SecureString confirmPassword) |
| | 5 | 50 | | { |
| | 5 | 51 | | ArgumentNullException.ThrowIfNull(password); |
| | 5 | 52 | | ArgumentNullException.ThrowIfNull(confirmPassword); |
| | | 53 | | |
| | 5 | 54 | | ErrorMessage = string.Empty; |
| | 5 | 55 | | if (!SecureEquals(password, confirmPassword)) |
| | 1 | 56 | | { |
| | 1 | 57 | | ErrorMessage = "Passwörter stimmen nicht überein."; |
| | 1 | 58 | | return; |
| | | 59 | | } |
| | 4 | 60 | | if (password.Length < 8) |
| | 1 | 61 | | { |
| | 1 | 62 | | ErrorMessage = "Passwort muss mindestens 8 Zeichen lang sein."; |
| | 1 | 63 | | return; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | try |
| | 3 | 67 | | { |
| | 3 | 68 | | AppServices.Current.MasterKeyManager.Initialize(password); |
| | 2 | 69 | | IsSetupMode = false; |
| | | 70 | | |
| | 2 | 71 | | var unlock = AppServices.Current.MasterKeyManager.Unlock(password) |
| | 2 | 72 | | ?? throw new InvalidOperationException("Neu angelegte Vault konnte nicht entsperrt werden."); |
| | 2 | 73 | | AppServices.Current.SessionManager.Open(unlock.VaultKey); |
| | 2 | 74 | | AppServices.Current.ActivityMonitor.Start(); |
| | 2 | 75 | | UnlockSucceeded?.Invoke(this, EventArgs.Empty); |
| | 2 | 76 | | } |
| | 1 | 77 | | catch (Exception ex) |
| | 1 | 78 | | { |
| | 1 | 79 | | ErrorMessage = MapError(ex); |
| | 1 | 80 | | } |
| | 5 | 81 | | } |
| | | 82 | | |
| | | 83 | | private static bool SecureEquals(SecureString left, SecureString right) |
| | 5 | 84 | | { |
| | 5 | 85 | | var secureStringService = new SecureStringService(); |
| | 5 | 86 | | var leftBytes = secureStringService.ToByteArray(left); |
| | 5 | 87 | | var rightBytes = secureStringService.ToByteArray(right); |
| | | 88 | | try |
| | 5 | 89 | | { |
| | 5 | 90 | | return System.Security.Cryptography.CryptographicOperations.FixedTimeEquals(leftBytes, rightBytes); |
| | | 91 | | } |
| | | 92 | | finally |
| | 5 | 93 | | { |
| | 5 | 94 | | secureStringService.ZeroMemory(leftBytes); |
| | 5 | 95 | | secureStringService.ZeroMemory(rightBytes); |
| | 5 | 96 | | } |
| | 5 | 97 | | } |
| | | 98 | | |
| | | 99 | | private static string MapError(Exception ex) |
| | 4 | 100 | | => ex switch |
| | 4 | 101 | | { |
| | 2 | 102 | | InvalidOperationException => ex.Message, |
| | 1 | 103 | | ArgumentException => ex.Message, |
| | 1 | 104 | | _ => "Der Vorgang konnte nicht abgeschlossen werden." |
| | 4 | 105 | | }; |
| | | 106 | | } |