# WinStd: include/WinStd/Cred.h Source File

Loading...
Searching...
No Matches
Cred.h
/*
SPDX-License-Identifier: MIT
Copyright © 1991-2024 Amebis
Copyright © 2016 GÉANT
*/
#pragma once
#include "Common.h"
#include <wincred.h>
#include <memory>
template<class _Traits, class _Ax>
static BOOL CredProtectA(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<char, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
{
char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
// Copy from stack.
sProtectedCredentials.assign(buf, dwSize - 1);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
sProtectedCredentials.resize(dwSize - 1);
if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, &sProtectedCredentials[0], &dwSize, ProtectionType))
return TRUE;
}
return FALSE;
}
template<class _Traits, class _Ax>
static BOOL CredProtectW(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
{
wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
// Copy from stack.
sProtectedCredentials.assign(buf, dwSize - 1);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
sProtectedCredentials.resize(dwSize - 1);
if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, &sProtectedCredentials[0], &dwSize, ProtectionType))
return TRUE;
}
return FALSE;
}
template<class _Traits, class _Ax>
static BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<char, _Traits, _Ax> &sCredentials)
{
char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
// Copy from stack.
sCredentials.assign(buf, dwSize);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
sCredentials.resize(dwSize - 1);
if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, &sCredentials[0], &dwSize))
return TRUE;
}
return FALSE;
}
template<class _Traits, class _Ax>
static BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sCredentials)
{
wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
// Copy from stack.
sCredentials.assign(buf, dwSize);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
sCredentials.resize(dwSize - 1);
if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, &sCredentials[0], &dwSize))
return TRUE;
}
return FALSE;
}
namespace winstd
{
template <class _Ty> struct CredFree_delete
{
typedef CredFree_delete<_Ty> _Myt;
CredFree_delete() {}
template <class _Ty2> CredFree_delete(const CredFree_delete<_Ty2>&) {}
void operator()(_Ty *_Ptr) const
{
CredFree(_Ptr);
}
};
template <class _Ty> struct CredFree_delete<_Ty[]>
{
typedef CredFree_delete<_Ty> _Myt;
CredFree_delete() {}
void operator()(_Ty *_Ptr) const noexcept
{
CredFree(_Ptr);
}
template<class _Other>
void operator()(_Other *) const
{
CredFree(_Ptr);
}
};
}
#pragma warning(push)
#pragma warning(disable: 4505) // Don't warn on unused code
static BOOL CredEnumerateA(_In_z_ LPCSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Inout_ std::unique_ptr<PCREDENTIALA[], winstd::CredFree_delete<PCREDENTIALA[]> > &cCredentials) noexcept
{
PCREDENTIALA *pCredentials;
if (CredEnumerateA(Filter, Flags, Count, &pCredentials)) {
cCredentials.reset(pCredentials);
return TRUE;
}
return FALSE;
}
static BOOL CredEnumerateW(_In_z_ LPCWSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Inout_ std::unique_ptr<PCREDENTIALW[], winstd::CredFree_delete<PCREDENTIALW[]> > &cCredentials) noexcept
{
PCREDENTIALW *pCredentials;
if (CredEnumerateW(Filter, Flags, Count, &pCredentials)) {
cCredentials.reset(pCredentials);
return TRUE;
}
return FALSE;
}
#pragma warning(pop)
CredUnprotectA
static BOOL CredUnprotectA(BOOL fAsSelf, LPCSTR pszProtectedCredentials, DWORD cchCredentials, std::basic_string< char, _Traits, _Ax > &sCredentials)
Decrypts credentials that were previously encrypted by using the CredProtect function.
Definition Cred.h:68
CredProtectA
static BOOL CredProtectA(BOOL fAsSelf, LPCSTR pszCredentials, DWORD cchCredentials, std::basic_string< char, _Traits, _Ax > &sProtectedCredentials, CRED_PROTECTION_TYPE *ProtectionType)
Encrypts the specified credentials so that only the current security context can decrypt them.
Definition Cred.h:20
CredEnumerateA
static BOOL CredEnumerateA(LPCSTR Filter, DWORD Flags, DWORD *Count, std::unique_ptr< PCREDENTIALA[], winstd::CredFree_delete< PCREDENTIALA[]> > &cCredentials) noexcept
Enumerates the credentials from the user's credential set. The credential set used is the one associa...
Definition Cred.h:193
CredEnumerateW
static BOOL CredEnumerateW(LPCWSTR Filter, DWORD Flags, DWORD *Count, std::unique_ptr< PCREDENTIALW[], winstd::CredFree_delete< PCREDENTIALW[]> > &cCredentials) noexcept
Enumerates the credentials from the user's credential set. The credential set used is the one associa...
Definition Cred.h:209
CredProtectW
static BOOL CredProtectW(BOOL fAsSelf, LPCWSTR pszCredentials, DWORD cchCredentials, std::basic_string< wchar_t, _Traits, _Ax > &sProtectedCredentials, CRED_PROTECTION_TYPE *ProtectionType)
Encrypts the specified credentials so that only the current security context can decrypt them.
Definition Cred.h:46
CredUnprotectW
static BOOL CredUnprotectW(BOOL fAsSelf, LPCWSTR pszProtectedCredentials, DWORD cchCredentials, std::basic_string< wchar_t, _Traits, _Ax > &sCredentials)
Decrypts credentials that were previously encrypted by using the CredProtect function.
Definition Cred.h:94
WINSTD_STACK_BUFFER_BYTES
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:94
winstd::CredFree_delete< _Ty[]>::_Myt
CredFree_delete< _Ty > _Myt
This type.
Definition Cred.h:154
winstd::CredFree_delete< _Ty[]>::CredFree_delete
CredFree_delete()
Default construct.
Definition Cred.h:159
winstd::CredFree_delete< _Ty[]>::operator()
void operator()(_Other *) const
Delete a pointer of another type.
Definition Cred.h:177
winstd::CredFree_delete< _Ty[]>::operator()
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Cred.h:166
winstd::CredFree_delete
Deleter for unique_ptr using CredFree.
Definition Cred.h:125
winstd::CredFree_delete::_Myt
CredFree_delete< _Ty > _Myt
This type.
Definition Cred.h:126
winstd::CredFree_delete::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Cred.h:143
winstd::CredFree_delete::CredFree_delete
CredFree_delete()
Default construct.
Definition Cred.h:131
winstd::CredFree_delete::CredFree_delete
CredFree_delete(const CredFree_delete< _Ty2 > &)
Construct from another CredFree_delete.
Definition Cred.h:136