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 std::unique_ptr<char[]> szBuffer(
new char[ulSize]);
38 if (::GetUserNameExA(NameFormat, szBuffer.get(), &ulSize)) {
39 sName.assign(szBuffer.get(), ulSize);
51template<
class _Traits,
class _Ax>
52static BOOLEAN GetUserNameExW(_In_ EXTENDED_NAME_FORMAT NameFormat, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sName)
57 ULONG ulSize = _countof(szStackBuffer);
60 if (::GetUserNameExW(NameFormat, szStackBuffer, &ulSize)) {
62 sName.assign(szStackBuffer, ulSize);
65 if (::GetLastError() == ERROR_MORE_DATA) {
67 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[ulSize]);
68 if (::GetUserNameExW(NameFormat, szBuffer.get(), &ulSize)) {
69 sName.assign(szBuffer.get(), ulSize);
140 if (
this != std::addressof(h)) {
157 _In_opt_ LPTSTR pszPrincipal,
158 _In_ LPTSTR pszPackage,
159 _In_
unsigned long fCredentialUse,
160 _In_opt_
void *pvLogonId,
161 _In_opt_
void *pAuthData,
162 _In_opt_ SEC_GET_KEY_FN pGetKeyFn = NULL,
163 _In_opt_
void *pvGetKeyArgument = NULL)
167 SECURITY_STATUS res = AcquireCredentialsHandle(pszPrincipal, pszPackage, fCredentialUse, pvLogonId, pAuthData, pGetKeyFn, pvGetKeyArgument, h, &exp);
168 if (SUCCEEDED(res)) {
184 FreeCredentialsHandle(
m_h);
203 handle<PCtxtHandle, NULL>()
237 if (
this != std::addressof(h)) {
255 _In_opt_ PCredHandle phCredential,
256 _In_opt_z_ LPCTSTR pszTargetName,
257 _In_ ULONG fContextReq,
258 _In_ ULONG TargetDataRep,
259 _In_opt_ PSecBufferDesc pInput,
260 _Inout_opt_ PSecBufferDesc pOutput)
267 SECURITY_STATUS res = InitializeSecurityContext(phCredential, NULL,
const_cast<LPTSTR
>(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, h, pOutput, &attr, &exp);
268 if (SUCCEEDED(res)) {
287 _In_opt_ PCredHandle phCredential,
288 _In_opt_z_ LPCTSTR pszTargetName,
289 _In_ ULONG fContextReq,
290 _In_ ULONG TargetDataRep,
291 _In_opt_ PSecBufferDesc pInput,
292 _Inout_opt_ PSecBufferDesc pOutput)
294 return InitializeSecurityContext(phCredential,
m_h,
const_cast<LPTSTR
>(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, NULL, pOutput, &
m_attrib, &
m_expires);
305 DeleteSecurityContext(
m_h);
323 sec_buffer_desc(_Inout_count_(count) PSecBuffer buf, ULONG count, _In_ ULONG version = SECBUFFER_VERSION)
337 for (ULONG i = 0; i < cBuffers; i++) {
338 if (pBuffers[i].pvBuffer)
339 FreeContextBuffer(pBuffers[i].pvBuffer);
Base abstract template class to support generic object handle keeping.
Definition Common.h:983
PCredHandle handle_type
Datatype of the object handle this template class handles.
Definition Common.h:988
handle_type m_h
Object handle.
Definition Common.h:1237
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1200
Numerical runtime error.
Definition Common.h:1446
SECURITY_STATUS error_type
Error number type.
Definition Common.h:1448
SecBufferDesc wrapper class.
Definition Sec.h:318
virtual ~sec_buffer_desc()
Frees the security buffer descriptor.
Definition Sec.h:335
sec_buffer_desc(PSecBuffer buf, ULONG count, ULONG version=SECBUFFER_VERSION)
Initializes security buffer descriptor.
Definition Sec.h:323
PCtxtHandle wrapper class.
Definition Sec.h:196
sec_context(sec_context &&h) noexcept
Move constructor.
Definition Sec.h:213
SECURITY_STATUS process(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Continue security context.
Definition Sec.h:286
virtual ~sec_context()
Frees the security context.
Definition Sec.h:224
sec_context()
Initializes a new class instance with the object handle set to NULL.
Definition Sec.h:201
SECURITY_STATUS initialize(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Initializes security context.
Definition Sec.h:254
ULONG m_attrib
Context attributes.
Definition Sec.h:310
TimeStamp m_expires
Context expiration time.
Definition Sec.h:311
sec_context & operator=(sec_context &&h) noexcept
Move assignment.
Definition Sec.h:235
void free_internal() noexcept override
Frees the security context.
Definition Sec.h:303
PCredHandle wrapper class.
Definition Sec.h:89
sec_credentials()
Initializes a new class instance with the object handle set to NULL.
Definition Sec.h:96
void free_internal() noexcept override
Frees the security credentials.
Definition Sec.h:182
TimeStamp m_expires
Credentials expiration time.
Definition Sec.h:189
sec_credentials(sec_credentials &&h) noexcept
Move constructor.
Definition Sec.h:117
virtual ~sec_credentials()
Frees the security credentials.
Definition Sec.h:127
sec_credentials(handle_type h, const TimeStamp expires)
Initializes a new class with an already available object handle.
Definition Sec.h:107
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:156
sec_credentials & operator=(sec_credentials &&h) noexcept
Move assignment.
Definition Sec.h:138
Security runtime error.
Definition Sec.h:355
sec_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Sec.h:372
sec_runtime_error(const sec_runtime_error &other)
Copies an exception.
Definition Sec.h:380
sec_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Sec.h:363
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:66
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:93
static const PCredHandle invalid
Invalid handle value.
Definition Common.h:993