WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
Cred.h
Go to the documentation of this file.
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
12
13#pragma once
14
15#include "Common.h"
16#include <wincred.h>
17#include <memory>
18
21
23template<class _Traits, class _Ax>
24static 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)
25{
26 char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
27 DWORD dwSize = _countof(buf);
28
29 // Try with the stack buffer first.
30 if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
31 // Copy from stack.
32 sProtectedCredentials.assign(buf, dwSize - 1);
33 return TRUE;
34 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
35 // Allocate on heap and retry.
36 std::unique_ptr<char[]> buf(new char[dwSize]);
37 if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
38 sProtectedCredentials.assign(buf.get(), dwSize - 1);
39 return TRUE;
40 }
41 }
42
43 return FALSE;
44}
45
51template<class _Traits, class _Ax>
52static 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)
53{
54 wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
55 DWORD dwSize = _countof(buf);
56
57 // Try with the stack buffer first.
58 if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
59 // Copy from stack.
60 sProtectedCredentials.assign(buf, dwSize - 1);
61 return TRUE;
62 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
63 // Allocate on heap and retry.
64 std::unique_ptr<wchar_t[]> buf(new wchar_t[dwSize]);
65 if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
66 sProtectedCredentials.assign(buf.get(), dwSize - 1);
67 return TRUE;
68 }
69 }
70
71 return FALSE;
72}
73
75template<class _Traits, class _Ax>
76static BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<char, _Traits, _Ax> &sCredentials)
77{
78 char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
79 DWORD dwSize = _countof(buf);
80
81 // Try with the stack buffer first.
82 if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
83 // Copy from stack.
84 sCredentials.assign(buf, dwSize);
85 return TRUE;
86 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
87 // Allocate on heap and retry.
88 std::unique_ptr<char[]> buf(new char[dwSize]);
89 if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
90 sCredentials.assign(buf.get(), dwSize);
91 return TRUE;
92 }
93 }
94
95 return FALSE;
96}
97
103template<class _Traits, class _Ax>
104static BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_count_(cchCredentials) LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sCredentials)
105{
106 wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
107 DWORD dwSize = _countof(buf);
108
109 // Try with the stack buffer first.
110 if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
111 // Copy from stack.
112 sCredentials.assign(buf, dwSize);
113 return TRUE;
114 } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
115 // Allocate on heap and retry.
116 std::unique_ptr<wchar_t[]> buf(new wchar_t[dwSize]);
117 if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
118 sCredentials.assign(buf.get(), dwSize);
119 return TRUE;
120 }
121 }
122
123 return FALSE;
124}
125
127
128namespace winstd
129{
132
136 template <class _Ty> struct CredFree_delete
137 {
139
144
148 template <class _Ty2> CredFree_delete(const CredFree_delete<_Ty2>&) {}
149
155 void operator()(_Ty *_Ptr) const
156 {
157 CredFree(_Ptr);
158 }
159 };
160
164 template <class _Ty> struct CredFree_delete<_Ty[]>
165 {
167
172
178 void operator()(_Ty *_Ptr) const noexcept
179 {
180 CredFree(_Ptr);
181 }
182
188 template<class _Other>
189 void operator()(_Other *) const
190 {
191 CredFree(_Ptr);
192 }
193 };
194
196}
197
200
206#pragma warning(suppress: 4505) // Don't warn on unused code
207static BOOL CredEnumerate(_In_z_ LPCTSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Inout_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials) noexcept
208{
209 PCREDENTIAL *pCredentials;
210 if (CredEnumerate(Filter, Flags, Count, &pCredentials)) {
211 cCredentials.reset(pCredentials);
212 return TRUE;
213 }
214
215 return FALSE;
216}
217
General API.
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:76
static BOOL CredEnumerate(LPCTSTR Filter, DWORD Flags, DWORD *Count, std::unique_ptr< PCREDENTIAL[], winstd::CredFree_delete< PCREDENTIAL[]> > &cCredentials) noexcept
Enumerates the credentials from the user's credential set. The credential set used is the one associa...
Definition: Cred.h:207
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:24
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:52
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:104
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:101
CredFree_delete< _Ty > _Myt
This type.
Definition: Cred.h:166
CredFree_delete()
Default construct.
Definition: Cred.h:171
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Cred.h:189
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Cred.h:178
Deleter for unique_ptr using CredFree.
Definition: Cred.h:137
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Cred.h:155
CredFree_delete()
Default construct.
Definition: Cred.h:143
CredFree_delete< _Ty > _Myt
This type.
Definition: Cred.h:138
CredFree_delete(const CredFree_delete< _Ty2 > &)
Construct from another CredFree_delete.
Definition: Cred.h:148