#include "DuoCredentialProvider.h" #include "DuoCredential.h" #include #include #ifndef CRED_PACK_PROTECTED_CREDENTIALS #define CRED_PACK_PROTECTED_CREDENTIALS 0x1 #endif #include #include static std::wstring StringFromPackedUnicodeString( const UNICODE_STRING& value, const BYTE* buffer, DWORD bufferSize) { if (!value.Buffer || value.Length == 0 || value.Length % sizeof(wchar_t) != 0) return {}; const auto start = reinterpret_cast(buffer); const auto end = start + bufferSize; const auto ptr = reinterpret_cast(value.Buffer); if (ptr < start || ptr + value.Length > end || ptr + value.Length < ptr) return {}; return std::wstring(value.Buffer, value.Length / sizeof(wchar_t)); } static bool TryUnpackKerbSerialization( const CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION* serialization, std::wstring& account, std::wstring& password) { account.clear(); password.clear(); if (!serialization || !serialization->rgbSerialization || serialization->cbSerialization < sizeof(KERB_INTERACTIVE_UNLOCK_LOGON)) { return false; } const auto* packedLogon = reinterpret_cast(serialization->rgbSerialization); if (packedLogon->Logon.MessageType != KerbInteractiveLogon && packedLogon->Logon.MessageType != KerbWorkstationUnlockLogon) { return false; } std::wstring domain = StringFromPackedUnicodeString( packedLogon->Logon.LogonDomainName, serialization->rgbSerialization, serialization->cbSerialization); std::wstring user = StringFromPackedUnicodeString( packedLogon->Logon.UserName, serialization->rgbSerialization, serialization->cbSerialization); password = StringFromPackedUnicodeString( packedLogon->Logon.Password, serialization->rgbSerialization, serialization->cbSerialization); if (user.empty()) return false; account = domain.empty() || user.find(L'\\') != std::wstring::npos || user.find(L'@') != std::wstring::npos ? user : domain + L"\\" + user; return true; } DuoCredentialProvider::DuoCredentialProvider() : _cRef(1), _scenario(CPUS_INVALID), _users(nullptr), _userCount(0), _hasSerialization(false) {} IFACEMETHODIMP DuoCredentialProvider::QueryInterface(REFIID riid, void** ppv) { if (!ppv) return E_INVALIDARG; *ppv = nullptr; if (riid == IID_IUnknown || riid == IID_ICredentialProvider) { *ppv = static_cast(this); AddRef(); return S_OK; } if (riid == IID_ICredentialProviderSetUserArray) { *ppv = static_cast(this); AddRef(); return S_OK; } return E_NOINTERFACE; } IFACEMETHODIMP_(ULONG) DuoCredentialProvider::AddRef() { return InterlockedIncrement(&_cRef); } IFACEMETHODIMP_(ULONG) DuoCredentialProvider::Release() { const long c = InterlockedDecrement(&_cRef); if (!c) { ClearCredentialCache(); if (_users) _users->Release(); delete this; } return c; } void DuoCredentialProvider::ClearCredentialCache() { for (DuoCredential* credential : _credentials) { if (credential) credential->Release(); } _credentials.clear(); } IFACEMETHODIMP DuoCredentialProvider::SetUsageScenario(CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus, DWORD) { if (_scenario != cpus) { ClearCredentialCache(); _scenario = cpus; } return S_OK; } IFACEMETHODIMP DuoCredentialProvider::SetSerialization(const CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION* pcpcs) { std::wstring serializedUsername; std::wstring serializedPassword; bool hasSerialization = false; auto applySerialization = [&]() { // LogonUI is allowed to repeat SetSerialization while it redraws a // Remote Desktop tile. Do not discard an in-progress OTP flow when // the supplied primary credential is byte-for-byte equivalent after // unpacking; only a genuinely new credential starts a new tile. if (_hasSerialization == hasSerialization && _serializedUsername == serializedUsername && _serializedPassword == serializedPassword) { return; } ClearCredentialCache(); _hasSerialization = hasSerialization; _serializedUsername = serializedUsername; _serializedPassword = serializedPassword; }; if (!pcpcs || !pcpcs->rgbSerialization || pcpcs->cbSerialization == 0) { applySerialization(); return S_OK; } DWORD userLen = 0; DWORD domainLen = 0; DWORD passwordLen = 0; DWORD flags = CRED_PACK_PROTECTED_CREDENTIALS; CredUnPackAuthenticationBufferW( flags, pcpcs->rgbSerialization, pcpcs->cbSerialization, nullptr, &userLen, nullptr, &domainLen, nullptr, &passwordLen); DWORD err = GetLastError(); if (err != ERROR_INSUFFICIENT_BUFFER) { flags = 0; userLen = 0; domainLen = 0; passwordLen = 0; CredUnPackAuthenticationBufferW( flags, pcpcs->rgbSerialization, pcpcs->cbSerialization, nullptr, &userLen, nullptr, &domainLen, nullptr, &passwordLen); err = GetLastError(); } if (err != ERROR_INSUFFICIENT_BUFFER || userLen == 0) { std::wstring account; std::wstring password; if (TryUnpackKerbSerialization(pcpcs, account, password)) { serializedUsername = account; serializedPassword = password; hasSerialization = !serializedUsername.empty(); } applySerialization(); return S_OK; } std::wstring user(userLen, L'\0'); DWORD domainCapacity = domainLen ? domainLen : 1; DWORD passwordCapacity = passwordLen ? passwordLen : 1; std::wstring domain(domainCapacity, L'\0'); std::wstring password(passwordCapacity, L'\0'); if (!CredUnPackAuthenticationBufferW( flags, pcpcs->rgbSerialization, pcpcs->cbSerialization, user.data(), &userLen, domain.data(), &domainCapacity, password.data(), &passwordCapacity)) { std::wstring account; std::wstring fallbackPassword; if (TryUnpackKerbSerialization(pcpcs, account, fallbackPassword)) { serializedUsername = account; serializedPassword = fallbackPassword; hasSerialization = !serializedUsername.empty(); } applySerialization(); return S_OK; } domainLen = domainCapacity; passwordLen = passwordCapacity; if (userLen && user[userLen - 1] == L'\0') --userLen; if (domainLen && domain[domainLen - 1] == L'\0') --domainLen; if (passwordLen && password[passwordLen - 1] == L'\0') --passwordLen; user.resize(userLen); domain.resize(domainLen); password.resize(passwordLen); serializedUsername = (!domain.empty() && user.find(L'\\') == std::wstring::npos && user.find(L'@') == std::wstring::npos) ? domain + L"\\" + user : user; serializedPassword = password; hasSerialization = !serializedUsername.empty(); applySerialization(); return S_OK; } IFACEMETHODIMP DuoCredentialProvider::Advise(ICredentialProviderEvents*, UINT_PTR) { return S_OK; } IFACEMETHODIMP DuoCredentialProvider::UnAdvise() { return S_OK; } IFACEMETHODIMP DuoCredentialProvider::GetFieldDescriptorCount(DWORD* pdwCount) { if (!pdwCount) return E_INVALIDARG; *pdwCount = 11; return S_OK; } IFACEMETHODIMP DuoCredentialProvider::GetFieldDescriptorAt(DWORD dwIndex, CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR** ppcpfd) { if (!ppcpfd || dwIndex >= 11) return E_INVALIDARG; *ppcpfd = (CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR*)CoTaskMemAlloc(sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR)); if (!*ppcpfd) return E_OUTOFMEMORY; ZeroMemory(*ppcpfd, sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR)); (*ppcpfd)->dwFieldID = dwIndex; switch (dwIndex) { case 0: (*ppcpfd)->cpft = CPFT_SMALL_TEXT; SHStrDupW(L"DUO-RDP 2FA", &(*ppcpfd)->pszLabel); break; case 1: (*ppcpfd)->cpft = CPFT_EDIT_TEXT; SHStrDupW(L"Username", &(*ppcpfd)->pszLabel); break; case 2: (*ppcpfd)->cpft = CPFT_PASSWORD_TEXT; SHStrDupW(L"Password", &(*ppcpfd)->pszLabel); break; case 3: (*ppcpfd)->cpft = CPFT_EDIT_TEXT; SHStrDupW(L"Password", &(*ppcpfd)->pszLabel); break; case 4: (*ppcpfd)->cpft = CPFT_CHECKBOX; SHStrDupW(L"Show password", &(*ppcpfd)->pszLabel); break; case 5: (*ppcpfd)->cpft = CPFT_COMBOBOX; SHStrDupW(L"2FA method", &(*ppcpfd)->pszLabel); break; case 6: (*ppcpfd)->cpft = CPFT_COMMAND_LINK; SHStrDupW(L"Отправить одноразовый код", &(*ppcpfd)->pszLabel); break; case 7: (*ppcpfd)->cpft = CPFT_SMALL_TEXT; SHStrDupW(L"В аккаунте не настроена 2FA авторизация", &(*ppcpfd)->pszLabel); break; case 8: (*ppcpfd)->cpft = CPFT_PASSWORD_TEXT; SHStrDupW(L"Одноразовый пароль", &(*ppcpfd)->pszLabel); break; case 9: (*ppcpfd)->cpft = CPFT_SUBMIT_BUTTON; SHStrDupW(L"Login", &(*ppcpfd)->pszLabel); break; case 10: (*ppcpfd)->cpft = CPFT_SMALL_TEXT; SHStrDupW(L"", &(*ppcpfd)->pszLabel); break; default: return E_INVALIDARG; } return S_OK; } IFACEMETHODIMP DuoCredentialProvider::GetCredentialCount(DWORD* pdwCount, DWORD* pdwDefault, BOOL* pbAutoLogonWithDefault) { if (!pdwCount || !pdwDefault || !pbAutoLogonWithDefault) return E_INVALIDARG; if (_scenario != CPUS_LOGON && _scenario != CPUS_UNLOCK_WORKSTATION && _scenario != CPUS_CREDUI) { *pdwCount = 0; *pdwDefault = CREDENTIAL_PROVIDER_NO_DEFAULT; *pbAutoLogonWithDefault = FALSE; return S_OK; } *pdwCount = _hasSerialization ? 1 : (_userCount + 1); *pdwDefault = (_userCount || _hasSerialization) ? 0 : CREDENTIAL_PROVIDER_NO_DEFAULT; // RDP can supply a serialized username/password before this provider has // evaluated its policy. Never auto-submit that tile: a 2FA-required // account must first render its OTP fields and wait for explicit input. *pbAutoLogonWithDefault = FALSE; return S_OK; } IFACEMETHODIMP DuoCredentialProvider::GetCredentialAt(DWORD dwIndex, ICredentialProviderCredential** ppcpc) { if (!ppcpc) return E_INVALIDARG; const DWORD count = _hasSerialization ? 1 : (_userCount + 1); if (dwIndex >= count) return E_INVALIDARG; if (_credentials.size() != count) { ClearCredentialCache(); _credentials.resize(count, nullptr); } DuoCredential* credential = _credentials[dwIndex]; if (!credential) { credential = new DuoCredential(); if (!credential) return E_OUTOFMEMORY; credential->SetUsageScenario(_scenario); if (!_hasSerialization && _users && dwIndex < _userCount) { ICredentialProviderUser* user = nullptr; if (SUCCEEDED(_users->GetAt(dwIndex, &user)) && user) { credential->Initialize(user); user->Release(); } } if (_hasSerialization) { credential->InitializeSerialized(_serializedUsername, _serializedPassword); } // Keep the constructor's initial reference as the provider-owned // cache reference. Each caller receives its own reference below. _credentials[dwIndex] = credential; } *ppcpc = credential; credential->AddRef(); return S_OK; } IFACEMETHODIMP DuoCredentialProvider::SetUserArray(ICredentialProviderUserArray* users) { if (users == _users) return S_OK; // An RDP serialized tile does not depend on the local user array. Keep // its in-progress OTP state if LogonUI refreshes that array underneath us. if (!_hasSerialization) ClearCredentialCache(); if (_users) { _users->Release(); _users = nullptr; } _userCount = 0; if (users) { _users = users; _users->AddRef(); _users->GetCount(&_userCount); } return S_OK; }