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
21template<class _Traits, class _Ax>
22static 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
49template<class _Traits, class _Ax>
50static 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
76namespace winstd
77{
80
84 class sec_credentials : public handle<PCredHandle, NULL>
85 {
87
88 public:
93 {
94 m_expires.QuadPart = -1;
95 }
96
107
114 m_expires(std::move(h.m_expires)),
115 handle<PCredHandle, NULL>(std::move(h))
116 {}
117
124 {
125 if (m_h != invalid)
127 }
128
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
171
172 protected:
178 void free_internal() noexcept override
179 {
181 delete m_h;
182 }
183
184 public:
186 };
187
191 class sec_context : public handle<PCtxtHandle, NULL>
192 {
193 public:
198 m_attrib(0),
200 {
201 m_expires.QuadPart = -1;
202 }
203
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)
224 }
225
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
272
292
293 protected:
299 void free_internal() noexcept override
300 {
302 delete m_h;
303 }
304
305 public:
308 };
309
313 class sec_buffer_desc : public SecBufferDesc
314 {
315 public:
325
332 {
333 for (ULONG i = 0; i < cBuffers; i++) {
334 if (pBuffers[i].pvBuffer)
336 }
337 }
338 };
339
341
344
350 class sec_runtime_error : public num_runtime_error<SECURITY_STATUS>
351 {
352 public:
361
370
378 };
379
381}
Base abstract template class to support generic object handle keeping.
Definition Common.h:1024
handle_type m_h
Object handle.
Definition Common.h:1276
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1239
Numerical runtime error.
Definition Common.h:1481
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:863
SecBufferDesc wrapper class.
Definition Sec.h:314
virtual ~sec_buffer_desc()
Frees the security buffer descriptor.
Definition Sec.h:331
sec_buffer_desc(PSecBuffer buf, ULONG count, ULONG version=SECBUFFER_VERSION)
Initializes security buffer descriptor.
Definition Sec.h:319
PCtxtHandle wrapper class.
Definition Sec.h:192
sec_context(sec_context &&h) noexcept
Move constructor.
Definition Sec.h:209
SECURITY_STATUS process(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Continue security context.
Definition Sec.h:282
virtual ~sec_context()
Frees the security context.
Definition Sec.h:220
sec_context()
Initializes a new class instance with the object handle set to NULL.
Definition Sec.h:197
SECURITY_STATUS initialize(PCredHandle phCredential, LPCTSTR pszTargetName, ULONG fContextReq, ULONG TargetDataRep, PSecBufferDesc pInput, PSecBufferDesc pOutput)
Initializes security context.
Definition Sec.h:250
ULONG m_attrib
Context attributes.
Definition Sec.h:306
TimeStamp m_expires
Context expiration time.
Definition Sec.h:307
sec_context & operator=(sec_context &&h) noexcept
Move assignment.
Definition Sec.h:231
void free_internal() noexcept override
Frees the security context.
Definition Sec.h:299
PCredHandle wrapper class.
Definition Sec.h:85
sec_credentials()
Initializes a new class instance with the object handle set to NULL.
Definition Sec.h:92
void free_internal() noexcept override
Frees the security credentials.
Definition Sec.h:178
TimeStamp m_expires
Credentials expiration time.
Definition Sec.h:185
sec_credentials(sec_credentials &&h) noexcept
Move constructor.
Definition Sec.h:113
virtual ~sec_credentials()
Frees the security credentials.
Definition Sec.h:123
sec_credentials(handle_type h, const TimeStamp expires)
Initializes a new class with an already available object handle.
Definition Sec.h:103
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
sec_credentials & operator=(sec_credentials &&h) noexcept
Move assignment.
Definition Sec.h:134
Security runtime error.
Definition Sec.h:351
sec_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Sec.h:368
sec_runtime_error(const sec_runtime_error &other)
Copies an exception.
Definition Sec.h:376
sec_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Sec.h:359
#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
Invalid handle value.
Definition Common.h:1034