Module: Lich::Common::GUI::MasterPasswordManager

Defined in:
documented/common/gui/master_password_manager.rb

Overview

Master password manager - Keychain integration for secure credential storage Provides one-time validation test creation and runtime password management CRITICAL: Validation test uses 100k iterations (one-time) Runtime decryption uses 10k iterations (via PasswordCipher)

Constant Summary collapse

KEYCHAIN_SERVICE =
'lich5.master_password'
VALIDATION_ITERATIONS =
100_000
VALIDATION_KEY_LENGTH =
32
VALIDATION_SALT_PREFIX =
'lich5-master-password-validation-v1'

Class Method Summary collapse

Class Method Details

.create_validation_test(master_password) ⇒ Hash

Creates a validation test (salt and hash) for master password verification.

Generates a random 16-byte salt and derives a validation key using PBKDF2-HMAC-SHA256 with 100,000 iterations. Computes a SHA256 digest of the key as the validation hash. Both salt and hash are Base64-encoded for storage.

Examples:

test = MasterPasswordManager.create_validation_test("mypassword")
test.keys #=> ["validation_salt", "validation_hash", "validation_version"]

Parameters:

  • master_password (String) —

    the master password to validate against

Returns:

  • (Hash) —

    a hash with keys 'validation_salt', 'validation_hash', and 'validation_version'



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'documented/common/gui/master_password_manager.rb', line 140

def self.create_validation_test(master_password)
  random_salt = SecureRandom.random_bytes(16)
  full_salt = VALIDATION_SALT_PREFIX + random_salt

  validation_key = OpenSSL::PKCS5.pbkdf2_hmac(
    master_password, full_salt, VALIDATION_ITERATIONS,
    VALIDATION_KEY_LENGTH, OpenSSL::Digest.new('SHA256')
  )

  validation_hash = OpenSSL::Digest::SHA256.digest(validation_key)

  {
    'validation_salt'    => Base64.strict_encode64(random_salt),
    'validation_hash'    => Base64.strict_encode64(validation_hash),
    'validation_version' => 1
  }
end

.delete_master_password ⇒ Boolean

Deletes the master password from the system keychain.

Delegates to the appropriate platform-specific backend (macOS, Linux, or Windows). Returns false if the keychain is unavailable or if the delete operation fails.

Examples:

MasterPasswordManager.delete_master_password #=> true

Returns:

  • (Boolean) —

    true if deletion succeeded, false otherwise or if keychain unavailable



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'documented/common/gui/master_password_manager.rb', line 203

def self.delete_master_password
  return false unless keychain_available?

  if OS.mac?
    delete_macos_keychain
  elsif OS.linux?
    delete_linux_keychain
  elsif OS.windows?
    delete_windows_keychain
  else
    false
  end
rescue StandardError => e
  Lich.log "error: Failed to delete master password: #{e.message}"
  false
end

.keychain_available? ⇒ Boolean

Checks whether the system keychain is available for credential storage.

Detects platform-specific keychain backends: macOS Keychain CLI, Linux secret-tool, or Windows Credential Manager.

Examples:

MasterPasswordManager.keychain_available? #=> true

Returns:

  • (Boolean) —

    true if a keychain backend is available and ready, false otherwise



35
36
37
38
39
40
41
42
43
44
45
# File 'documented/common/gui/master_password_manager.rb', line 35

def self.keychain_available?
  if OS.mac?
    macos_keychain_available?
  elsif OS.linux?
    linux_keychain_available?
  elsif OS.windows?
    windows_keychain_available?
  else
    false
  end
end

.retrieve_master_password(strict: false) ⇒ Object

Strict reads are required for transaction snapshots and rollback checks: nil means confirmed absence, while unavailable or failed reads raise. Legacy callers retain the nil-on-error contract.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'documented/common/gui/master_password_manager.rb', line 76

def self.retrieve_master_password(strict: false)
  return retrieve_master_password_strict if strict

  return nil unless keychain_available?

  if OS.mac?
    retrieve_macos_keychain
  elsif OS.linux?
    retrieve_linux_keychain
  elsif OS.windows?
    retrieve_windows_keychain
  else
    nil
  end
rescue StandardError => e
  raise if strict

  Lich.log "error: Failed to retrieve master password: #{e.message}"
  nil
end

.store_master_password(master_password) ⇒ Boolean

Stores the master password in the system keychain.

Delegates to the appropriate platform-specific backend (macOS, Linux, or Windows). Returns false if the keychain is unavailable or if the store operation fails.

Examples:

MasterPasswordManager.store_master_password("mypassword") #=> true

Parameters:

  • master_password (String) —

    the master password to store

Returns:

  • (Boolean) —

    true if storage succeeded, false otherwise or if keychain unavailable



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'documented/common/gui/master_password_manager.rb', line 56

def self.store_master_password(master_password)
  return false unless keychain_available?

  if OS.mac?
    store_macos_keychain(master_password)
  elsif OS.linux?
    store_linux_keychain(master_password)
  elsif OS.windows?
    store_windows_keychain(master_password)
  else
    false
  end
rescue StandardError => e
  Lich.log "error: Failed to store master password: #{e.message}"
  false
end

.validate_master_password(entered_password, validation_test) ⇒ Boolean

Validates an entered password against a stored validation test.

Decodes the stored Base64-encoded salt and hash, derives a validation key from the entered password using the same PBKDF2-HMAC-SHA256 parameters (100,000 iterations), and performs a constant-time comparison of the computed hash against the stored hash. Returns false if the validation_test is malformed, missing required fields, or if comparison fails.

Examples:

test = MasterPasswordManager.create_validation_test("mypassword")
MasterPasswordManager.validate_master_password("mypassword", test) #=> true
MasterPasswordManager.validate_master_password("wrong", test) #=> false

Parameters:

  • entered_password (String) —

    the password entered by the user

  • validation_test (Hash) —

    the validation test hash from .create_validation_test

Returns:

  • (Boolean) —

    true if the password matches, false otherwise or if validation_test is invalid



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'documented/common/gui/master_password_manager.rb', line 173

def self.validate_master_password(entered_password, validation_test)
  return false unless validation_test.is_a?(Hash)
  return false unless validation_test['validation_salt'] && validation_test['validation_hash']

  begin
    random_salt = Base64.strict_decode64(validation_test['validation_salt'])
    stored_hash = Base64.strict_decode64(validation_test['validation_hash'])
    full_salt = VALIDATION_SALT_PREFIX + random_salt

    validation_key = OpenSSL::PKCS5.pbkdf2_hmac(
      entered_password, full_salt, VALIDATION_ITERATIONS,
      VALIDATION_KEY_LENGTH, OpenSSL::Digest.new('SHA256')
    )

    computed_hash = OpenSSL::Digest::SHA256.digest(validation_key)
    secure_compare(computed_hash, stored_hash)
  rescue StandardError => e
    Lich.log "error: Validation failed: #{e.message}"
    false
  end
end