18#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL)
21template<
class _Traits,
class _Ax>
22static BOOLEAN GetUserNameExA(_In_ EXTENDED_NAME_FORMAT NameFormat, _Inout_ std::basic_string<char, _Traits, _Ax> &sName)
27 ULONG ulSize = _countof(szStackBuffer);
30 if (::GetUserNameExA(NameFormat, szStackBuffer, &ulSize)) {
32 sName.assign(szStackBuffer, ulSize);
35 if (::GetLastError() == ERROR_MORE_DATA) {
37 sName.resize(ulSize - 1);
38 if (::GetUserNameExA(NameFormat, &ulSize[0], &ulSize))
49template<
class _Traits,
class _Ax>
50static BOOLEAN GetUserNameExW(_In_ EXTENDED_NAME_FORMAT NameFormat, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sName)
53 ULONG ulSize = _countof(szStackBuffer);
56 if (::GetUserNameExW(NameFormat, szStackBuffer, &ulSize)) {
58 sName.assign(szStackBuffer, ulSize);
61 if (::GetLastError() == ERROR_MORE_DATA) {
63 sName.resize(ulSize - 1);
64 if (::GetUserNameExW(NameFormat, &sName[0], &ulSize))
134 if (
this != std::addressof(h)) {
151 _In_opt_ LPTSTR pszPrincipal,
152 _In_ LPTSTR pszPackage,
153 _In_
unsigned long fCredentialUse,
154 _In_opt_
void *pvLogonId,
155 _In_opt_
void *pAuthData,
156 _In_opt_ SEC_GET_KEY_FN pGetKeyFn = NULL,
157 _In_opt_
void *pvGetKeyArgument = NULL)
161 SECURITY_STATUS res = AcquireCredentialsHandle(pszPrincipal, pszPackage, fCredentialUse, pvLogonId, pAuthData, pGetKeyFn, pvGetKeyArgument, h, &exp);
162 if (SUCCEEDED(res)) {
178 FreeCredentialsHandle(
m_h);
197 handle<PCtxtHandle, NULL>()
231 if (
this != std::addressof(h)) {
249 _In_opt_ PCredHandle phCredential,
250 _In_opt_z_ LPCTSTR pszTargetName,
251 _In_ ULONG fContextReq,
252 _In_ ULONG TargetDataRep,
253 _In_opt_ PSecBufferDesc pInput,
254 _Inout_opt_ PSecBufferDesc pOutput)
261 SECURITY_STATUS res = InitializeSecurityContext(phCredential, NULL,
const_cast<LPTSTR
>(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, h, pOutput, &attr, &exp);
262 if (SUCCEEDED(res)) {
281 _In_opt_ PCredHandle phCredential,
282 _In_opt_z_ LPCTSTR pszTargetName,
283 _In_ ULONG fContextReq,
284 _In_ ULONG TargetDataRep,
285 _In_opt_ PSecBufferDesc pInput,
286 _Inout_opt_ PSecBufferDesc pOutput)
288 return InitializeSecurityContext(phCredential,
m_h,
const_cast<LPTSTR
>(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, NULL, pOutput, &
m_attrib, &
m_expires);
299 DeleteSecurityContext(
m_h);
317 sec_buffer_desc(_Inout_count_(count) PSecBuffer buf, ULONG count, _In_ ULONG version = SECBUFFER_VERSION)
331 for (ULONG i = 0; i < cBuffers; i++) {
332 if (pBuffers[i].pvBuffer)
333 FreeContextBuffer(pBuffers[i].pvBuffer);
Base abstract template class to support generic object handle keeping.
Definition Common.h:874
handle_type m_h
Definition Common.h:1126
void attach(handle_type h) noexcept
Definition Common.h:1089
PCredHandle handle_type
Definition Common.h:879
Numerical runtime error.
Definition Common.h:1331
SECURITY_STATUS error_type
Definition Common.h:1333
SecBufferDesc wrapper class.
Definition Sec.h:312
virtual ~sec_buffer_desc()
Frees the security buffer descriptor.
Definition Sec.h:329
sec_buffer_desc(PSecBuffer buf, ULONG count, ULONG version=SECBUFFER_VERSION)
Initializes security buffer descriptor.
Definition Sec.h:317
PCtxtHandle wrapper class.
Definition Sec.h:190
sec_context(sec_context &&h) noexcept
Move constructor.
Definition Sec.h:207
SECURITY_STATUS process(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Continue security context.
Definition Sec.h:280
virtual ~sec_context()
Frees the security context.
Definition Sec.h:218
sec_context()
Initializes a new class instance with the object handle set to NULL.
Definition Sec.h:195
SECURITY_STATUS initialize(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Initializes security context.
Definition Sec.h:248
ULONG m_attrib
Context attributes.
Definition Sec.h:304
TimeStamp m_expires
Context expiration time.
Definition Sec.h:305
sec_context & operator=(sec_context &&h) noexcept
Move assignment.
Definition Sec.h:229
void free_internal() noexcept override
Frees the security context.
Definition Sec.h:297
PCredHandle wrapper class.
Definition Sec.h:83
sec_credentials()
Initializes a new class instance with the object handle set to NULL.
Definition Sec.h:90
void free_internal() noexcept override
Frees the security credentials.
Definition Sec.h:176
TimeStamp m_expires
Credentials expiration time.
Definition Sec.h:183
sec_credentials(sec_credentials &&h) noexcept
Move constructor.
Definition Sec.h:111
virtual ~sec_credentials()
Frees the security credentials.
Definition Sec.h:121
sec_credentials(handle_type h, const TimeStamp expires)
Initializes a new class with an already available object handle.
Definition Sec.h:101
SECURITY_STATUS acquire(LPTSTR pszPrincipal, LPTSTR pszPackage, unsigned long fCredentialUse, void *pvLogonId, void *pAuthData, SEC_GET_KEY_FN pGetKeyFn=NULL, void *pvGetKeyArgument=NULL)
Acquires the security credentials.
Definition Sec.h:150
sec_credentials & operator=(sec_credentials &&h) noexcept
Move assignment.
Definition Sec.h:132
Security runtime error.
Definition Sec.h:349
sec_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Sec.h:366
sec_runtime_error(const sec_runtime_error &other)
Copies an exception.
Definition Sec.h:374
sec_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Sec.h:357
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:67
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:94
static const PCredHandle invalid
Definition Common.h:884