WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
Cred.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 <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 sProtectedCredentials.resize(dwSize - 1);
33 if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, &sProtectedCredentials[0], &dwSize, ProtectionType))
34 return TRUE;
35 }
36
37 return FALSE;
38}
39
45template<class _Traits, class _Ax>
46static 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)
47{
48 wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
49 DWORD dwSize = _countof(buf);
50
51 // Try with the stack buffer first.
52 if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
53 // Copy from stack.
54 sProtectedCredentials.assign(buf, dwSize - 1);
55 return TRUE;
56 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
57 // Allocate on heap and retry.
58 sProtectedCredentials.resize(dwSize - 1);
59 if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, &sProtectedCredentials[0], &dwSize, ProtectionType))
60 return TRUE;
61 }
62
63 return FALSE;
64}
65
67template<class _Traits, class _Ax>
68static BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<char, _Traits, _Ax> &sCredentials)
69{
70 char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
71 DWORD dwSize = _countof(buf);
72
73 // Try with the stack buffer first.
74 if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
75 // Copy from stack.
76 sCredentials.assign(buf, dwSize);
77 return TRUE;
78 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
79 // Allocate on heap and retry.
80 sCredentials.resize(dwSize - 1);
81 if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, &sCredentials[0], &dwSize))
82 return TRUE;
83 }
84
85 return FALSE;
86}
87
93template<class _Traits, class _Ax>
94static BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sCredentials)
95{
96 wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
97 DWORD dwSize = _countof(buf);
98
99 // Try with the stack buffer first.
100 if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
101 // Copy from stack.
102 sCredentials.assign(buf, dwSize);
103 return TRUE;
104 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
105 // Allocate on heap and retry.
106 sCredentials.resize(dwSize - 1);
107 if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, &sCredentials[0], &dwSize))
108 return TRUE;
109 }
110
111 return FALSE;
112}
113
115
116namespace winstd
117{
120
124 template <class _Ty> struct CredFree_delete
125 {
127
132
136 template <class _Ty2> CredFree_delete(const CredFree_delete<_Ty2>&) {}
137
143 void operator()(_Ty *_Ptr) const
144 {
145 CredFree(_Ptr);
146 }
147 };
148
152 template <class _Ty> struct CredFree_delete<_Ty[]>
153 {
155
160
166 void operator()(_Ty *_Ptr) const noexcept
167 {
168 CredFree(_Ptr);
169 }
170
176 template<class _Other>
177 void operator()(_Other *) const
178 {
179 CredFree(_Ptr);
180 }
181 };
182
184}
185
188
189#pragma warning(push)
190#pragma warning(disable: 4505) // Don't warn on unused code
191
193static BOOL CredEnumerateA(_In_z_ LPCSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Inout_ std::unique_ptr<PCREDENTIALA[], winstd::CredFree_delete<PCREDENTIALA[]> > &cCredentials) noexcept
194{
195 PCREDENTIALA *pCredentials;
196 if (CredEnumerateA(Filter, Flags, Count, &pCredentials)) {
197 cCredentials.reset(pCredentials);
198 return TRUE;
199 }
200
201 return FALSE;
202}
203
209static BOOL CredEnumerateW(_In_z_ LPCWSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Inout_ std::unique_ptr<PCREDENTIALW[], winstd::CredFree_delete<PCREDENTIALW[]> > &cCredentials) noexcept
210{
211 PCREDENTIALW *pCredentials;
212 if (CredEnumerateW(Filter, Flags, Count, &pCredentials)) {
213 cCredentials.reset(pCredentials);
214 return TRUE;
215 }
216
217 return FALSE;
218}
219
220#pragma warning(pop)
221
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:68
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:193
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:209
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:46
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:94
#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:154
CredFree_delete()
Default construct.
Definition Cred.h:159
void operator()(_Other *) const
Delete a pointer of another type.
Definition Cred.h:177
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Cred.h:166
Deleter for unique_ptr using CredFree.
Definition Cred.h:125
CredFree_delete< _Ty > _Myt
This type.
Definition Cred.h:126
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Cred.h:143
CredFree_delete()
Default construct.
Definition Cred.h:131
CredFree_delete(const CredFree_delete< _Ty2 > &)
Construct from another CredFree_delete.
Definition Cred.h:136