19#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL)
22template<
class _Traits,
class _Ax>
23static BOOLEAN GetUserNameExA(_In_ EXTENDED_NAME_FORMAT NameFormat, _Inout_ std::basic_string<char, _Traits, _Ax> &sName)
28 ULONG ulSize = _countof(szStackBuffer);
31 if (::GetUserNameExA(NameFormat, szStackBuffer, &ulSize)) {
33 sName.assign(szStackBuffer, ulSize);
36 if (::GetLastError() == ERROR_MORE_DATA) {
38 std::unique_ptr<char[]> szBuffer(
new char[ulSize]);
39 if (::GetUserNameExA(NameFormat, szBuffer.get(), &ulSize)) {
40 sName.assign(szBuffer.get(), ulSize);
54template<
class _Traits,
class _Ax>
55static BOOLEAN GetUserNameExW(_In_ EXTENDED_NAME_FORMAT NameFormat, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sName)
60 ULONG ulSize = _countof(szStackBuffer);
63 if (::GetUserNameExW(NameFormat, szStackBuffer, &ulSize)) {
65 sName.assign(szStackBuffer, ulSize);
68 if (::GetLastError() == ERROR_MORE_DATA) {
70 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[ulSize]);
71 if (::GetUserNameExW(NameFormat, szBuffer.get(), &ulSize)) {
72 sName.assign(szBuffer.get(), ulSize);
147 if (
this != std::addressof(h)) {
164 _In_opt_ LPTSTR pszPrincipal,
165 _In_ LPTSTR pszPackage,
166 _In_
unsigned long fCredentialUse,
167 _In_opt_
void *pvLogonId,
168 _In_opt_
void *pAuthData,
169 _In_opt_ SEC_GET_KEY_FN pGetKeyFn = NULL,
170 _In_opt_
void *pvGetKeyArgument = NULL)
174 SECURITY_STATUS res = AcquireCredentialsHandle(pszPrincipal, pszPackage, fCredentialUse, pvLogonId, pAuthData, pGetKeyFn, pvGetKeyArgument, h, &exp);
175 if (SUCCEEDED(res)) {
191 FreeCredentialsHandle(
m_h);
210 handle<PCtxtHandle, NULL>()
245 if (
this != std::addressof(h)) {
263 _In_opt_ PCredHandle phCredential,
264 _In_opt_z_ LPCTSTR pszTargetName,
265 _In_ ULONG fContextReq,
266 _In_ ULONG TargetDataRep,
267 _In_opt_ PSecBufferDesc pInput,
268 _Inout_opt_ PSecBufferDesc pOutput)
275 SECURITY_STATUS res = InitializeSecurityContext(phCredential, NULL,
const_cast<LPTSTR
>(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, h, pOutput, &attr, &exp);
276 if (SUCCEEDED(res)) {
295 _In_opt_ PCredHandle phCredential,
296 _In_opt_z_ LPCTSTR pszTargetName,
297 _In_ ULONG fContextReq,
298 _In_ ULONG TargetDataRep,
299 _In_opt_ PSecBufferDesc pInput,
300 _Inout_opt_ PSecBufferDesc pOutput)
302 return InitializeSecurityContext(phCredential,
m_h,
const_cast<LPTSTR
>(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, NULL, pOutput, &
m_attrib, &
m_expires);
313 DeleteSecurityContext(
m_h);
331 sec_buffer_desc(_Inout_count_(count) PSecBuffer buf, ULONG count, _In_ ULONG version = SECBUFFER_VERSION)
345 for (ULONG i = 0; i < cBuffers; i++) {
346 if (pBuffers[i].pvBuffer)
347 FreeContextBuffer(pBuffers[i].pvBuffer);
Base abstract template class to support generic object handle keeping.
Definition: Common.h:604
PCredHandle handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:609
handle_type m_h
Object handle.
Definition: Common.h:855
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:818
Numerical runtime error.
Definition: Common.h:1003
SECURITY_STATUS error_type
Error number type.
Definition: Common.h:1005
SecBufferDesc wrapper class.
Definition: Sec.h:326
virtual ~sec_buffer_desc()
Frees the security buffer descriptor.
Definition: Sec.h:343
sec_buffer_desc(PSecBuffer buf, ULONG count, ULONG version=SECBUFFER_VERSION)
Initializes security buffer descriptor.
Definition: Sec.h:331
PCtxtHandle wrapper class.
Definition: Sec.h:203
sec_context(sec_context &&h) noexcept
Move constructor.
Definition: Sec.h:220
SECURITY_STATUS process(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Continue security context.
Definition: Sec.h:294
virtual ~sec_context()
Frees the security context.
Definition: Sec.h:232
sec_context()
Initializes a new class instance with the object handle set to NULL.
Definition: Sec.h:208
SECURITY_STATUS initialize(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Initializes security context.
Definition: Sec.h:262
ULONG m_attrib
Context attributes.
Definition: Sec.h:318
TimeStamp m_expires
Context expiration time.
Definition: Sec.h:319
sec_context & operator=(sec_context &&h) noexcept
Move assignment.
Definition: Sec.h:243
void free_internal() noexcept override
Frees the security context.
Definition: Sec.h:311
PCredHandle wrapper class.
Definition: Sec.h:94
sec_credentials()
Initializes a new class instance with the object handle set to NULL.
Definition: Sec.h:101
void free_internal() noexcept override
Frees the security credentials.
Definition: Sec.h:189
TimeStamp m_expires
Credentials expiration time.
Definition: Sec.h:196
sec_credentials(sec_credentials &&h) noexcept
Move constructor.
Definition: Sec.h:123
virtual ~sec_credentials()
Frees the security credentials.
Definition: Sec.h:134
sec_credentials(handle_type h, const TimeStamp expires)
Initializes a new class with an already available object handle.
Definition: Sec.h:112
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:163
sec_credentials & operator=(sec_credentials &&h) noexcept
Move assignment.
Definition: Sec.h:145
Security runtime error.
Definition: Sec.h:363
sec_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Sec.h:381
sec_runtime_error(const sec_runtime_error &other)
Copies an exception.
Definition: Sec.h:390
sec_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Sec.h:371
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:53
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:80
static const PCredHandle invalid
Invalid handle value.
Definition: Common.h:614