WinStd Windows Win32 API using Standard C++

Loading...
Searching...
No Matches
Sec.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2024 Amebis
4 Copyright © 2016 GÉANT
5*/
6
8
9#pragma once
10
11#include "Common.h"
12#include <Security.h>
13#include <string>
14
17
18#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL)
19
21 template<class _Traits, class _Ax>
22 static BOOLEAN GetUserNameExA(_In_ EXTENDED_NAME_FORMAT NameFormat, _Inout_ std::basic_string<char, _Traits, _Ax> &sName)
23{
24 assert(0); // TODO: Test this code.
25
26 char szStackBuffer[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
27 ULONG ulSize = _countof(szStackBuffer);
28
29 // Try with stack buffer first.
30 if (::GetUserNameExA(NameFormat, szStackBuffer, &ulSize)) {
31 // Copy from stack.
32 sName.assign(szStackBuffer, ulSize);
33 return TRUE;
34 }
35 if (::GetLastError() == ERROR_MORE_DATA) {
36 // Allocate buffer on heap and retry.
37 sName.resize(ulSize - 1);
38 if (::GetUserNameExA(NameFormat, &ulSize[0], &ulSize))
39 return TRUE;
40 }
41 return FALSE;
42}
43
49 template<class _Traits, class _Ax>
50 static BOOLEAN GetUserNameExW(_In_ EXTENDED_NAME_FORMAT NameFormat, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sName)
51{
52 assert(0); // TODO: Test this code.
53
54 wchar_t szStackBuffer[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
55 ULONG ulSize = _countof(szStackBuffer);
56
57 // Try with stack buffer first.
58 if (::GetUserNameExW(NameFormat, szStackBuffer, &ulSize)) {
59 // Copy from stack.
60 sName.assign(szStackBuffer, ulSize);
61 return TRUE;
62 }
63 if (::GetLastError() == ERROR_MORE_DATA) {
64 // Allocate buffer on heap and retry.
65 sName.resize(ulSize - 1);
66 if (::GetUserNameExW(NameFormat, &sName[0], &ulSize))
67 return TRUE;
68 }
69 return FALSE;
70}
71
72#endif
73
75
76 namespace winstd
77{
80
84 class sec_credentials : public handle<PCredHandle, NULL>
85 {
86 WINSTD_NONCOPYABLE(sec_credentials)
87
88 public:
92 sec_credentials()
93 {
94 m_expires.QuadPart = -1;
95 }
96
103 sec_credentials(_In_opt_ handle_type h, _In_ const TimeStamp expires) :
104 m_expires(expires),
105 handle(h)
106 {}
107
113 sec_credentials(_Inout_ sec_credentials &&h) noexcept :
114 m_expires(std::move(h.m_expires)),
115 handle<PCredHandle, NULL>(std::move(h))
116 {}
117
123 virtual ~sec_credentials()
124 {
125 if (m_h != invalid)
126 free_internal();
127 }
128
134 sec_credentials& operator=(_Inout_ sec_credentials &&h) noexcept
135 {
136 if (this != std::addressof(h)) {
137 *(handle<handle_type, NULL>*)this = std::move(h);
138 m_expires = std::move(h.m_expires);
139 }
140 return *this;
141 }
142
152 SECURITY_STATUS acquire(
153 _In_opt_ LPTSTR pszPrincipal,
154 _In_ LPTSTR pszPackage,
155 _In_ unsigned long fCredentialUse,
156 _In_opt_ void *pvLogonId,
157 _In_opt_ void *pAuthData,
158 _In_opt_ SEC_GET_KEY_FN pGetKeyFn = NULL,
159 _In_opt_ void *pvGetKeyArgument = NULL)
160 {
161 handle_type h = new CredHandle;
162 TimeStamp exp;
163 SECURITY_STATUS res = AcquireCredentialsHandle(pszPrincipal, pszPackage, fCredentialUse, pvLogonId, pAuthData, pGetKeyFn, pvGetKeyArgument, h, &exp);
164 if (SUCCEEDED(res)) {
165 attach(h);
166 m_expires = exp;
167 } else
168 delete h;
169 return res;
170 }
171
172 protected:
178 void free_internal() noexcept override
179 {
180 FreeCredentialsHandle(m_h);
181 delete m_h;
182 }
183
184 public:
185 TimeStamp m_expires;
186 };
187
191 class sec_context : public handle<PCtxtHandle, NULL>
192 {
193 public:
197 sec_context() :
198 m_attrib(0),
199 handle<PCtxtHandle, NULL>()
200 {
201 m_expires.QuadPart = -1;
202 }
203
209 sec_context(_Inout_ sec_context &&h) noexcept :
210 m_attrib (std::move(h.m_attrib )),
211 m_expires(std::move(h.m_expires)),
212 handle<PCtxtHandle, NULL>(std::move(h))
213 {}
214
220 virtual ~sec_context()
221 {
222 if (m_h != invalid)
223 free_internal();
224 }
225
231 sec_context& operator=(_Inout_ sec_context &&h) noexcept
232 {
233 if (this != std::addressof(h)) {
234 *(handle<handle_type, NULL>*)this = std::move(h);
235 m_attrib = std::move(h.m_attrib);
236 m_expires = std::move(h.m_expires);
237 }
238 return *this;
239 }
240
250 SECURITY_STATUS initialize(
251 _In_opt_ PCredHandle phCredential,
252 _In_opt_z_ LPCTSTR pszTargetName,
253 _In_ ULONG fContextReq,
254 _In_ ULONG TargetDataRep,
255 _In_opt_ PSecBufferDesc pInput,
256 _Inout_opt_ PSecBufferDesc pOutput)
257 {
258 handle_type h = new CtxtHandle;
259 h->dwUpper = 0;
260 h->dwLower = 0;
261 ULONG attr;
262 TimeStamp exp;
263 SECURITY_STATUS res = InitializeSecurityContext(phCredential, NULL, const_cast<LPTSTR>(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, h, pOutput, &attr, &exp);
264 if (SUCCEEDED(res)) {
265 attach(h);
266 m_attrib = attr;
267 m_expires = exp;
268 } else
269 delete h;
270 return res;
271 }
272
282 SECURITY_STATUS process(
283 _In_opt_ PCredHandle phCredential,
284 _In_opt_z_ LPCTSTR pszTargetName,
285 _In_ ULONG fContextReq,
286 _In_ ULONG TargetDataRep,
287 _In_opt_ PSecBufferDesc pInput,
288 _Inout_opt_ PSecBufferDesc pOutput)
289 {
290 return InitializeSecurityContext(phCredential, m_h, const_cast<LPTSTR>(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, NULL, pOutput, &m_attrib, &m_expires);
291 }
292
293 protected:
299 void free_internal() noexcept override
300 {
301 DeleteSecurityContext(m_h);
302 delete m_h;
303 }
304
305 public:
306 ULONG m_attrib;
307 TimeStamp m_expires;
308 };
309
313 class sec_buffer_desc : public SecBufferDesc
314 {
315 public:
319 sec_buffer_desc(_Inout_count_(count) PSecBuffer buf, ULONG count, _In_ ULONG version = SECBUFFER_VERSION)
320 {
321 ulVersion = version;
322 cBuffers = count;
323 pBuffers = buf;
324 }
325
331 virtual ~sec_buffer_desc()
332 {
333 for (ULONG i = 0; i < cBuffers; i++) {
334 if (pBuffers[i].pvBuffer)
335 FreeContextBuffer(pBuffers[i].pvBuffer);
336 }
337 }
338 };
339
341
344
350 class sec_runtime_error : public num_runtime_error<SECURITY_STATUS>
351 {
352 public:
359 sec_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<SECURITY_STATUS>(num, msg)
360 {}
361
368 sec_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<SECURITY_STATUS>(num, msg)
369 {}
370
376 sec_runtime_error(const sec_runtime_error &other) : num_runtime_error<SECURITY_STATUS>(other)
377 {}
378 };
379
381}
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition Common.h:1024
winstd::handle< PCredHandle, NULL >::m_h
handle_type m_h
Object handle.
Definition Common.h:1276
winstd::handle< PCredHandle, NULL >::attach
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1239
winstd::num_runtime_error
Numerical runtime error.
Definition Common.h:1481
winstd::ref_unique_ptr
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:863
winstd::sec_buffer_desc
SecBufferDesc wrapper class.
Definition Sec.h:314
winstd::sec_buffer_desc::~sec_buffer_desc
virtual ~sec_buffer_desc()
Frees the security buffer descriptor.
Definition Sec.h:331
winstd::sec_buffer_desc::sec_buffer_desc
sec_buffer_desc(PSecBuffer buf, ULONG count, ULONG version=SECBUFFER_VERSION)
Initializes security buffer descriptor.
Definition Sec.h:319
winstd::sec_context
PCtxtHandle wrapper class.
Definition Sec.h:192
winstd::sec_context::sec_context
sec_context(sec_context &&h) noexcept
Move constructor.
Definition Sec.h:209
winstd::sec_context::process
SECURITY_STATUS process(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Continue security context.
Definition Sec.h:282
winstd::sec_context::~sec_context
virtual ~sec_context()
Frees the security context.
Definition Sec.h:220
winstd::sec_context::sec_context
sec_context()
Initializes a new class instance with the object handle set to NULL.
Definition Sec.h:197
winstd::sec_context::initialize
SECURITY_STATUS initialize(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Initializes security context.
Definition Sec.h:250
winstd::sec_context::m_attrib
ULONG m_attrib
Context attributes.
Definition Sec.h:306
winstd::sec_context::m_expires
TimeStamp m_expires
Context expiration time.
Definition Sec.h:307
winstd::sec_context::operator=
sec_context & operator=(sec_context &&h) noexcept
Move assignment.
Definition Sec.h:231
winstd::sec_context::free_internal
void free_internal() noexcept override
Frees the security context.
Definition Sec.h:299
winstd::sec_credentials
PCredHandle wrapper class.
Definition Sec.h:85
winstd::sec_credentials::sec_credentials
sec_credentials()
Initializes a new class instance with the object handle set to NULL.
Definition Sec.h:92
winstd::sec_credentials::free_internal
void free_internal() noexcept override
Frees the security credentials.
Definition Sec.h:178
winstd::sec_credentials::m_expires
TimeStamp m_expires
Credentials expiration time.
Definition Sec.h:185
winstd::sec_credentials::sec_credentials
sec_credentials(sec_credentials &&h) noexcept
Move constructor.
Definition Sec.h:113
winstd::sec_credentials::~sec_credentials
virtual ~sec_credentials()
Frees the security credentials.
Definition Sec.h:123
winstd::sec_credentials::sec_credentials
sec_credentials(handle_type h, const TimeStamp expires)
Initializes a new class with an already available object handle.
Definition Sec.h:103
winstd::sec_credentials::acquire
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:152
winstd::sec_credentials::operator=
sec_credentials & operator=(sec_credentials &&h) noexcept
Move assignment.
Definition Sec.h:134
winstd::sec_runtime_error
Security runtime error.
Definition Sec.h:351
winstd::sec_runtime_error::sec_runtime_error
sec_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Sec.h:368
winstd::sec_runtime_error::sec_runtime_error
sec_runtime_error(const sec_runtime_error &other)
Copies an exception.
Definition Sec.h:376
winstd::sec_runtime_error::sec_runtime_error
sec_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Sec.h:359
WINSTD_NONCOPYABLE
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:67
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::handle< PCredHandle, NULL >::invalid
static const PCredHandle invalid
Invalid handle value.
Definition Common.h:1034