Skip to content
This repository was archived by the owner on Nov 29, 2024. It is now read-only.

Commit 641c9b6

Browse files
committed
Credentials are no longer stored using method name (TLS/PAP/MSCHAPv2) but with level/type identifier
1 parent b11cb3a commit 641c9b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+226
-604
lines changed

CredWrite/Main.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static int CredWrite()
4040
return -1;
4141
}
4242

43-
eap::credentials_pap cred_pap(g_module);
43+
eap::credentials_pass cred_pass(g_module);
4444

4545
// Prepare identity (user name).
4646
{
@@ -50,7 +50,7 @@ static int CredWrite()
5050
bool is_last;
5151
dec.decode(identity_utf8, is_last, pwcArglist[1], (size_t)-1);
5252

53-
MultiByteToWideChar(CP_UTF8, 0, identity_utf8.data(), (int)identity_utf8.size(), cred_pap.m_identity);
53+
MultiByteToWideChar(CP_UTF8, 0, identity_utf8.data(), (int)identity_utf8.size(), cred_pass.m_identity);
5454
}
5555

5656
// Prepare password.
@@ -61,7 +61,7 @@ static int CredWrite()
6161
bool is_last;
6262
dec.decode(password_utf8, is_last, pwcArglist[2], (size_t)-1);
6363

64-
MultiByteToWideChar(CP_UTF8, 0, password_utf8.data(), (int)password_utf8.size(), cred_pap.m_password);
64+
MultiByteToWideChar(CP_UTF8, 0, password_utf8.data(), (int)password_utf8.size(), cred_pass.m_password);
6565
}
6666

6767
// Generate target name (aka realm).
@@ -71,20 +71,30 @@ static int CredWrite()
7171
target_name = pwcArglist[3];
7272
} else {
7373
// Get the realm from user name.
74-
LPCWSTR _identity = cred_pap.m_identity.c_str(), domain;
74+
LPCWSTR _identity = cred_pass.m_identity.c_str(), domain;
7575
if ((domain = wcschr(_identity, L'@')) != NULL) {
7676
target_name = L"urn:RFC4282:realm:";
7777
target_name += domain + 1;
7878
} else
7979
target_name = L"*";
8080
}
8181

82+
// Determine credential level.
83+
unsigned int level;
84+
if (nArgs > 4) {
85+
// User explicitly set the level.
86+
level = wcstoul(pwcArglist[4], NULL, 10);
87+
} else {
88+
// Set default level.
89+
level = 0;
90+
}
91+
8292
// Write credentials.
8393
#ifdef _DEBUG
8494
{
85-
eap::credentials_pap cred_stored(g_module);
95+
eap::credentials_pass cred_stored(g_module);
8696
try {
87-
cred_stored.retrieve(target_name.c_str());
97+
cred_stored.retrieve(target_name.c_str(), level);
8898
} catch(win_runtime_error &err) {
8999
OutputDebugStr(_T("%hs (error %u)\n"), err.what(), err.number());
90100
} catch(...) {
@@ -93,7 +103,7 @@ static int CredWrite()
93103
}
94104
#endif
95105
try {
96-
cred_pap.store(target_name.c_str());
106+
cred_pass.store(target_name.c_str(), level);
97107
} catch(win_runtime_error &err) {
98108
OutputDebugStr(_T("%hs (error %u)\n"), err.what(), err.number());
99109
return 2;
@@ -102,18 +112,6 @@ static int CredWrite()
102112
return 2;
103113
}
104114

105-
// Store empty TLS credentials.
106-
eap::credentials_tls cred_tls(g_module);
107-
try {
108-
cred_tls.store(target_name.c_str());
109-
} catch(win_runtime_error &err) {
110-
OutputDebugStr(_T("%hs (error %u)\n"), err.what(), err.number());
111-
return 3;
112-
} catch(...) {
113-
OutputDebugStr(_T("Writing credentials failed.\n"));
114-
return 3;
115-
}
116-
117115
return 0;
118116
}
119117

CredWrite/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ Imports given credentials to Windows Credential Manager for G
33

44
##Usage
55
```
6-
CredWrite <username> <password> [<realm>]
6+
CredWrite <username> <password> [<realm> [level]]
77
```
88

99
- `username` - Base64 encoded UTF-8 user name (usually of the form user@domain or domain\user)
1010
- `password` - Base64 encoded UTF-8 user password
1111
- `realm` - A realm ID to allow grouping of credentials over different WLAN profiles (optional, default is domain part of `username`)
12+
- `level` - Credential level (0=outer, 1=inner, 2=inner-inner..., default is 0=outer)
1213

1314
The credentials are stored to Windows Credential Manager in invoking user's roaming profile.
1415

CredWrite/StdAfx.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
#pragma once
2222

23-
#include "../lib/PAP/include/Credentials.h"
24-
#include "../lib/TLS/include/Credentials.h"
23+
#include "../lib/EAPBase/include/Credentials.h"
2524
#include "../lib/EAPBase/include/Module.h"
2625

2726
#include <WinStd/Common.h>

lib/EAPBase/include/Config.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ namespace eap
205205
///
206206
/// Constructs configuration
207207
///
208-
/// \param[in] mod EAP module to use for global services
208+
/// \param[in] mod EAP module to use for global services
209+
/// \param[in] level Config level (0=outer, 1=inner, 2=inner-inner...)
209210
///
210-
config_method(_In_ module &mod);
211+
config_method(_In_ module &mod, _In_ unsigned int level);
211212

212213
///
213214
/// Copies configuration
@@ -252,6 +253,9 @@ namespace eap
252253
/// Returns a string identifier of the EAP method type of this configuration
253254
///
254255
virtual const wchar_t* get_method_str() const = 0;
256+
257+
public:
258+
const unsigned int m_level; ///< Config level (0=outer, 1=inner, 2=inner-inner...)
255259
};
256260

257261

@@ -264,9 +268,10 @@ namespace eap
264268
///
265269
/// Constructs configuration
266270
///
267-
/// \param[in] mod EAP module to use for global services
271+
/// \param[in] mod EAP module to use for global services
272+
/// \param[in] level Config level (0=outer, 1=inner, 2=inner-inner...)
268273
///
269-
config_method_with_cred(_In_ module &mod);
274+
config_method_with_cred(_In_ module &mod, _In_ unsigned int level);
270275

271276
///
272277
/// Copies configuration

lib/EAPBase/include/Credentials.h

+52-5
Original file line numberDiff line numberDiff line change
@@ -179,28 +179,40 @@ namespace eap
179179
/// Save credentials to Windows Credential Manager
180180
///
181181
/// \param[in] pszTargetName The name in Windows Credential Manager to store credentials as
182+
/// \param[in] level Credential level (0=outer, 1=inner, 2=inner-inner...)
182183
///
183-
virtual void store(_In_z_ LPCTSTR pszTargetName) const = 0;
184+
virtual void store(_In_z_ LPCTSTR pszTargetName, _In_ unsigned int level) const = 0;
184185

185186
///
186187
/// Retrieve credentials from Windows Credential Manager
187188
///
188189
/// \param[in] pszTargetName The name in Windows Credential Manager to retrieve credentials from
190+
/// \param[in] level Credential level (0=outer, 1=inner, 2=inner-inner...)
189191
///
190-
virtual void retrieve(_In_z_ LPCTSTR pszTargetName) = 0;
192+
virtual void retrieve(_In_z_ LPCTSTR pszTargetName, _In_ unsigned int level) = 0;
191193

192194
///
193195
/// Returns target name for Windows Credential Manager credential name
194196
///
195197
/// \param[in] pszTargetName The name in Windows Credential Manager to retrieve credentials from
198+
/// \param[in] level Credential level (0=outer, 1=inner, 2=inner-inner...)
196199
///
197200
/// \returns Final target name to store/retrieve credentials in Windows Credential Manager
198201
///
199-
inline winstd::tstring target_name(_In_z_ LPCTSTR pszTargetName) const
202+
inline winstd::tstring target_name(_In_z_ LPCTSTR pszTargetName, _In_ unsigned int level) const
200203
{
204+
// Start with product name and given target name (identity provider usually).
201205
winstd::tstring target_name(_T(PRODUCT_NAME_STR) _T("/"));
202206
target_name += pszTargetName;
203207
target_name += _T('/');
208+
209+
// Append level of credentials.
210+
TCHAR buf[20];
211+
_ultot_s(level, buf, _countof(buf), 10);
212+
target_name += buf;
213+
target_name += _T('/');
214+
215+
// Append credential type.
204216
target_name += target_suffix();
205217
assert(target_name.length() < CRED_MAX_GENERIC_TARGET_NAME_LENGTH);
206218
return target_name;
@@ -291,6 +303,13 @@ namespace eap
291303
///
292304
credentials_pass& operator=(_Inout_ credentials_pass &&other);
293305

306+
///
307+
/// Clones credentials
308+
///
309+
/// \returns Pointer to cloned credentials
310+
///
311+
virtual config* clone() const;
312+
294313
///
295314
/// Resets credentials
296315
///
@@ -358,18 +377,46 @@ namespace eap
358377
/// Save credentials to Windows Credential Manager
359378
///
360379
/// \param[in] pszTargetName The name in Windows Credential Manager to store credentials as
380+
/// \param[in] level Credential level (0=outer, 1=inner, 2=inner-inner...)
361381
///
362-
virtual void store(_In_z_ LPCTSTR pszTargetName) const;
382+
virtual void store(_In_z_ LPCTSTR pszTargetName, _In_ unsigned int level) const;
363383

364384
///
365385
/// Retrieve credentials from Windows Credential Manager
366386
///
367387
/// \param[in] pszTargetName The name in Windows Credential Manager to retrieve credentials from
388+
/// \param[in] level Credential level (0=outer, 1=inner, 2=inner-inner...)
368389
///
369-
virtual void retrieve(_In_z_ LPCTSTR pszTargetName);
390+
virtual void retrieve(_In_z_ LPCTSTR pszTargetName, _In_ unsigned int level);
391+
392+
///
393+
/// Return target suffix for Windows Credential Manager credential name
394+
///
395+
virtual LPCTSTR target_suffix() const;
370396

371397
/// @}
372398

399+
///
400+
/// Combine credentials in the following order:
401+
///
402+
/// 1. Cached credentials
403+
/// 2. Pre-configured credentials
404+
/// 3. Stored credentials
405+
///
406+
/// \param[in] cred_cached Cached credentials (optional, can be \c NULL, must be credentials_pass* type)
407+
/// \param[in] cfg Method configuration (must be config_method_pap type)
408+
/// \param[in] pszTargetName The name in Windows Credential Manager to retrieve credentials from (optional, can be \c NULL)
409+
///
410+
/// \returns
411+
/// - \c source_cache Credentials were obtained from EapHost cache
412+
/// - \c source_preshared Credentials were set by method configuration
413+
/// - \c source_storage Credentials were loaded from Windows Credential Manager
414+
///
415+
virtual source_t combine(
416+
_In_ const credentials *cred_cached,
417+
_In_ const config_method_with_cred &cfg,
418+
_In_opt_z_ LPCTSTR pszTargetName);
419+
373420
public:
374421
winstd::sanitizing_wstring m_password; ///< Password
375422

lib/EAPBase/src/Config.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -102,34 +102,44 @@ const bstr eap::config::namespace_eapmetadata(L"urn:ietf:params:xml:ns:yang:ietf
102102
// eap::config_method
103103
//////////////////////////////////////////////////////////////////////
104104

105-
eap::config_method::config_method(_In_ module &mod) : config(mod)
105+
eap::config_method::config_method(_In_ module &mod, _In_ unsigned int level) :
106+
m_level(level),
107+
config(mod)
106108
{
107109
}
108110

109111

110-
eap::config_method::config_method(_In_ const config_method &other) : config(other)
112+
eap::config_method::config_method(_In_ const config_method &other) :
113+
m_level(other.m_level),
114+
config(other)
111115
{
112116
}
113117

114118

115-
eap::config_method::config_method(_Inout_ config_method &&other) : config(std::move(other))
119+
eap::config_method::config_method(_Inout_ config_method &&other) :
120+
m_level(other.m_level),
121+
config(std::move(other))
116122
{
117123
}
118124

119125

120126
eap::config_method& eap::config_method::operator=(_In_ const config_method &other)
121127
{
122-
if (this != &other)
128+
if (this != &other) {
129+
assert(m_level == other.m_level); // Allow copy within same configuration level only.
123130
(config&)*this = other;
131+
}
124132

125133
return *this;
126134
}
127135

128136

129137
eap::config_method& eap::config_method::operator=(_Inout_ config_method &&other)
130138
{
131-
if (this != &other)
139+
if (this != &other) {
140+
assert(m_level == other.m_level); // Allow move within same configuration level only.
132141
(config&&)*this = std::move(other);
142+
}
133143

134144
return *this;
135145
}
@@ -139,11 +149,11 @@ eap::config_method& eap::config_method::operator=(_Inout_ config_method &&other)
139149
// eap::config_method_with_cred
140150
//////////////////////////////////////////////////////////////////////
141151

142-
eap::config_method_with_cred::config_method_with_cred(_In_ module &mod) :
152+
eap::config_method_with_cred::config_method_with_cred(_In_ module &mod, _In_ unsigned int level) :
143153
m_allow_save(true),
144154
m_use_preshared(false),
145155
m_last_status(status_success),
146-
config_method(mod)
156+
config_method(mod, level)
147157
{
148158
}
149159

0 commit comments

Comments
 (0)