Module: Lich::Common::GUI::WindowsCredentialManager

Extended by:
FFI::Library
Defined in:
documented/common/gui/windows_credential_manager.rb

Overview

Windows Credential Manager integration via FFI REQUIRES: ffi gem (>= 1.15) must be available in Lich environment Provides direct access to Windows Credential Manager API for secure credential storage Supports generic credentials (passwords) and certificate credentials (SSH keys)

Uses DPAPI (Data Protection API) via Credential Manager for encryption No PowerShell subprocess calls - direct Win32 API access

Defined Under Namespace

Classes: CredentialStruct

Constant Summary collapse

CRED_TYPE_GENERIC =

CRED_TYPE values

1
CRED_TYPE_DOMAIN_PASSWORD =
2
CRED_TYPE_DOMAIN_CERTIFICATE =
3
CRED_TYPE_DOMAIN_VISIBLE_PASSWORD =
4
CRED_TYPE_GENERIC_CERTIFICATE =
5
CRED_TYPE_DOMAIN_EXTENDED =
6
CRED_PERSIST_SESSION =

CRED_PERSIST values

1
CRED_PERSIST_LOCAL_MACHINE =
2
CRED_PERSIST_ENTERPRISE =
3
CRED_MAX_CREDENTIAL_BLOB_SIZE =

Max credential size (512KB)

512 * 1024

Class Method Summary collapse

Class Method Details

.available? ⇒ Boolean

Check if Credential Manager is available

Returns:

  • (Boolean) —

    true if Credential Manager is accessible



79
80
81
82
83
84
85
86
87
88
89
# File 'documented/common/gui/windows_credential_manager.rb', line 79

def available?
  return false unless OS.windows?

  # Simple test: try to allocate credential structure
  begin
    FFI::MemoryPointer.new(CredentialStruct)
    true
  rescue
    false
  end
end

.delete_credential(target_name) ⇒ Boolean

Delete a credential from Credential Manager

Parameters:

  • target_name (String) —

    Target/service name to delete

Returns:

  • (Boolean) —

    true if credential deleted successfully



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'documented/common/gui/windows_credential_manager.rb', line 198

def delete_credential(target_name)
  return false unless available?

  begin
    target_name_wide = string_to_wide(target_name)

    # Call CredDeleteW
    result = CredDeleteW(target_name_wide, CRED_TYPE_GENERIC, 0)

    if result
      true
    else
      error_code = FFI::LastError.winapi_error
      Lich.log "error: CredDeleteW failed with error code #{error_code}"
      false
    end
  rescue StandardError => e
    Lich.log "error: Failed to delete credential: #{e.message}"
    false
  end
end

.retrieve_credential(target_name, strict: false) ⇒ String?

Retrieve a generic credential from Credential Manager

Parameters:

  • target_name (String) —

    Target/service name to retrieve

  • strict (Boolean) (defaults to: false) —

    raise for failures other than confirmed absence

Returns:

  • (String, nil) —

    Retrieved password/secret, or nil if not found



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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/windows_credential_manager.rb', line 151

def retrieve_credential(target_name, strict: false)
  unless available?
    raise IOError, "Windows Credential Manager unavailable" if strict
    return nil
  end

  begin
    target_name_wide = string_to_wide(target_name)
    cred_ptr = FFI::MemoryPointer.new(:pointer)

    # Call CredReadW
    result = CredReadW(target_name_wide, CRED_TYPE_GENERIC, 0, cred_ptr)

    if result
      cred = cred_ptr.read_pointer
      credential = CredentialStruct.new(cred)

      # Extract password blob
      blob_ptr = credential[:credential_blob]
      blob_size = credential[:credential_blob_size]
      password_blob = blob_ptr.read_bytes(blob_size)
      # Password blob is stored as UTF-8 bytes; convert safely with encoding handling
      password = password_blob.b.force_encoding('UTF-8')

      # Free credential structure
      CredFree(cred)

      password
    else
      error_code = FFI::LastError.winapi_error
      raise IOError, "CredReadW failed with error code #{error_code}" if strict && error_code != 1168
      # ERROR_NOT_FOUND = 1168, so only log unexpected errors
      Lich.log "error: CredReadW failed with error code #{error_code}" unless error_code == 1168
      nil
    end
  rescue StandardError
    raise if strict

    # Don't log as error for "not found" - it's expected on first run
    # Only actual failures (encoding, access, API errors) should log
    nil
  end
end

.store_credential(target_name, username, password, comment = nil, persist = CRED_PERSIST_LOCAL_MACHINE) ⇒ Boolean

Store a generic credential (password) in Credential Manager

Parameters:

  • target_name (String) —

    Target/service name (e.g., 'lich5.master_password')

  • username (String) —

    Username associated with credential

  • password (String) —

    Password/secret to store

  • comment (String, nil) (defaults to: nil) —

    Optional comment/description

  • persist (Integer) (defaults to: CRED_PERSIST_LOCAL_MACHINE) —

    Persistence level (1=session, 2=local_machine, 3=enterprise)

Returns:

  • (Boolean) —

    true if credential stored successfully



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'documented/common/gui/windows_credential_manager.rb', line 98

def store_credential(target_name, username, password, comment = nil, persist = CRED_PERSIST_LOCAL_MACHINE)
  return false unless available?

  begin
    credential = CredentialStruct.new

    # Convert strings to wide characters (UTF-16LE)
    target_name_wide = string_to_wide(target_name)
    username_wide = string_to_wide(username)
    # Store password as UTF-8 bytes (as binary data, not text)
    password_bytes = password.to_s.encode('UTF-8')
    password_blob = password_bytes.b
    comment_wide = comment ? string_to_wide(comment) : FFI::Pointer.new(:pointer, 0)

    Lich.log "debug: Storing credential - target: #{target_name}, user: #{username}, pass_size: #{password_blob.bytesize}, persist: #{persist}"

    # Allocate memory for credential blob (password data)
    blob_ptr = FFI::MemoryPointer.new(:uint8, password_blob.bytesize)
    blob_ptr.put_bytes(0, password_blob)

    # Fill credential structure
    credential[:flags] = 0
    credential[:type] = CRED_TYPE_GENERIC
    credential[:target_name] = target_name_wide
    credential[:comment] = comment_wide
    credential[:credential_blob_size] = password_blob.size
    credential[:credential_blob] = blob_ptr
    credential[:persist] = persist
    credential[:attribute_count] = 0
    credential[:attributes] = FFI::Pointer.new(:pointer, 0)
    credential[:user_name] = username_wide

    # Call CredWriteW - pass pointer to the credential struct
    result = CredWriteW(credential, 0)

    if result
      Lich.log "debug: Credential stored successfully"
      true
    else
      error_code = FFI::LastError.winapi_error
      Lich.log "error: CredWriteW failed with error code #{error_code}"
      false
    end
  rescue StandardError => e
    Lich.log "error: Failed to store credential: #{e.class.name}: #{e.message}"
    false
  end
end