WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
Cred.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2023 Amebis
4 Copyright © 2016 GÉANT
5*/
6
8
9#pragma once
10
11#include "Common.h"
12#include <wincred.h>
13#include <memory>
14
17
19template<class _Traits, class _Ax>
20static BOOL CredProtectA(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<char, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
21{
22 char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
23 DWORD dwSize = _countof(buf);
24
25 // Try with the stack buffer first.
26 if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
27 // Copy from stack.
28 sProtectedCredentials.assign(buf, dwSize - 1);
29 return TRUE;
30 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
31 // Allocate on heap and retry.
32 std::unique_ptr<char[]> buf(new char[dwSize]);
33 if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
34 sProtectedCredentials.assign(buf.get(), dwSize - 1);
35 return TRUE;
36 }
37 }
38
39 return FALSE;
40}
41
47template<class _Traits, class _Ax>
48static BOOL CredProtectW(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
49{
50 wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
51 DWORD dwSize = _countof(buf);
52
53 // Try with the stack buffer first.
54 if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
55 // Copy from stack.
56 sProtectedCredentials.assign(buf, dwSize - 1);
57 return TRUE;
58 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
59 // Allocate on heap and retry.
60 std::unique_ptr<wchar_t[]> buf(new wchar_t[dwSize]);
61 if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
62 sProtectedCredentials.assign(buf.get(), dwSize - 1);
63 return TRUE;
64 }
65 }
66
67 return FALSE;
68}
69
71template<class _Traits, class _Ax>
72static BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<char, _Traits, _Ax> &sCredentials)
73{
74 char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
75 DWORD dwSize = _countof(buf);
76
77 // Try with the stack buffer first.
78 if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
79 // Copy from stack.
80 sCredentials.assign(buf, dwSize);
81 return TRUE;
82 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
83 // Allocate on heap and retry.
84 std::unique_ptr<char[]> buf(new char[dwSize]);
85 if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
86 sCredentials.assign(buf.get(), dwSize);
87 return TRUE;
88 }
89 }
90
91 return FALSE;
92}
93
99template<class _Traits, class _Ax>
100static BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sCredentials)
101{
102 wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
103 DWORD dwSize = _countof(buf);
104
105 // Try with the stack buffer first.
106 if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
107 // Copy from stack.
108 sCredentials.assign(buf, dwSize);
109 return TRUE;
110 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
111 // Allocate on heap and retry.
112 std::unique_ptr<wchar_t[]> buf(new wchar_t[dwSize]);
113 if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
114 sCredentials.assign(buf.get(), dwSize);
115 return TRUE;
116 }
117 }
118
119 return FALSE;
120}
121
123
124namespace winstd
125{
128
132 template <class _Ty> struct CredFree_delete
133 {
135
140
144 template <class _Ty2> CredFree_delete(const CredFree_delete<_Ty2>&) {}
145
151 void operator()(_Ty *_Ptr) const
152 {
153 CredFree(_Ptr);
154 }
155 };
156
160 template <class _Ty> struct CredFree_delete<_Ty[]>
161 {
163
168
174 void operator()(_Ty *_Ptr) const noexcept
175 {
176 CredFree(_Ptr);
177 }
178
184 template<class _Other>
185 void operator()(_Other *) const
186 {
187 CredFree(_Ptr);
188 }
189 };
190
192}
193
196
197#pragma warning(push)
198#pragma warning(disable: 4505) // Don't warn on unused code
199
201static BOOL CredEnumerateA(_In_z_ LPCSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Inout_ std::unique_ptr<PCREDENTIALA[], winstd::CredFree_delete<PCREDENTIALA[]> > &cCredentials) noexcept
202{
203 PCREDENTIALA *pCredentials;
204 if (CredEnumerateA(Filter, Flags, Count, &pCredentials)) {
205 cCredentials.reset(pCredentials);
206 return TRUE;
207 }
208
209 return FALSE;
210}
211
217static BOOL CredEnumerateW(_In_z_ LPCWSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Inout_ std::unique_ptr<PCREDENTIALW[], winstd::CredFree_delete<PCREDENTIALW[]> > &cCredentials) noexcept
218{
219 PCREDENTIALW *pCredentials;
220 if (CredEnumerateW(Filter, Flags, Count, &pCredentials)) {
221 cCredentials.reset(pCredentials);
222 return TRUE;
223 }
224
225 return FALSE;
226}
227
228#pragma warning(pop)
229
static BOOL CredUnprotectA(BOOL fAsSelf, LPCSTR pszProtectedCredentials, DWORD cchCredentials, std::basic_string< char, _Traits, _Ax > &sCredentials)
Decrypts credentials that were previously encrypted by using the CredProtect function.
Definition Cred.h:72
static BOOL CredProtectA(BOOL fAsSelf, LPCSTR pszCredentials, DWORD cchCredentials, std::basic_string< char, _Traits, _Ax > &sProtectedCredentials, CRED_PROTECTION_TYPE *ProtectionType)
Encrypts the specified credentials so that only the current security context can decrypt them.
Definition Cred.h:20
static BOOL CredEnumerateA(LPCSTR Filter, DWORD Flags, DWORD *Count, std::unique_ptr< PCREDENTIALA[], winstd::CredFree_delete< PCREDENTIALA[]> > &cCredentials) noexcept
Enumerates the credentials from the user's credential set. The credential set used is the one associa...
Definition Cred.h:201
static BOOL CredEnumerateW(LPCWSTR Filter, DWORD Flags, DWORD *Count, std::unique_ptr< PCREDENTIALW[], winstd::CredFree_delete< PCREDENTIALW[]> > &cCredentials) noexcept
Enumerates the credentials from the user's credential set. The credential set used is the one associa...
Definition Cred.h:217
static BOOL CredProtectW(BOOL fAsSelf, LPCWSTR pszCredentials, DWORD cchCredentials, std::basic_string< wchar_t, _Traits, _Ax > &sProtectedCredentials, CRED_PROTECTION_TYPE *ProtectionType)
Encrypts the specified credentials so that only the current security context can decrypt them.
Definition Cred.h:48
static BOOL CredUnprotectW(BOOL fAsSelf, LPCWSTR pszProtectedCredentials, DWORD cchCredentials, std::basic_string< wchar_t, _Traits, _Ax > &sCredentials)
Decrypts credentials that were previously encrypted by using the CredProtect function.
Definition Cred.h:100
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:94
CredFree_delete< _Ty > _Myt
This type.
Definition Cred.h:162
CredFree_delete()
Default construct.
Definition Cred.h:167
void operator()(_Other *) const
Delete a pointer of another type.
Definition Cred.h:185
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Cred.h:174
Deleter for unique_ptr using CredFree.
Definition Cred.h:133
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Cred.h:151
CredFree_delete()
Default construct.
Definition Cred.h:139
CredFree_delete< _Ty > _Myt
This type.
Definition Cred.h:134
CredFree_delete(const CredFree_delete< _Ty2 > &)
Construct from another CredFree_delete.
Definition Cred.h:144