-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialStore.cpp
More file actions
312 lines (282 loc) · 7.78 KB
/
Copy pathCredentialStore.cpp
File metadata and controls
312 lines (282 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <shlobj.h>
#include <algorithm>
#include <cctype>
#include <filesystem>
#include <fstream>
#include "CredentialStore.h"
#include "Encoding.h"
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "ole32.lib")
// File format (UTF-8 text, one [credential] block per entry, binary values as hex):
//
// [credential]
// rpId = example.phraselock.com
// userName = alice
// userId = 3f2a1b9c...
// credentialId = a17c55e0...
// publicKey = e1d2...
// aaguid = 2fc0579f811347eab116bb5a8db9202a
// signCount = 5
// created = 2026-09-25 14:32:10
//
// Lines starting with '#' are comments. Unknown keys are ignored, so the format can be extended.
namespace fs = std::filesystem;
using Encoding::FromHex;
namespace
{
constexpr wchar_t STORE_DIR[] = L"PhraseLock\\PLP-FIDO-Example";
constexpr wchar_t STORE_FILE[] = L"credentials.txt";
const char FILE_HEADER[] =
"# PLP FIDO Example - credential store\n"
"#\n"
"# One [credential] block per registered credential. Binary values are hex:\n"
"# userId random user handle, sent to the authenticator as user.id\n"
"# credentialId chosen by the authenticator, sent back in the allow list on sign-in\n"
"# publicKey ES256 (ECDSA P-256) public key, x || y (64 bytes) - verifies sign-in signatures\n"
"# aaguid authenticator model id (16 bytes, all zero if not disclosed)\n"
"# signCount is the last signature counter seen (decimal) - it must increase on every sign-in,\n"
"# otherwise the authenticator may have been cloned.\n";
std::string Trim(const std::string& s)
{
size_t b = s.find_first_not_of(" \t\n");
if (b == std::string::npos)
{
return {};
}
size_t e = s.find_last_not_of(" \t\n");
return s.substr(b, e - b + 1);
}
std::wstring StorePath()
{
PWSTR appData = nullptr;
std::wstring path;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &appData)))
{
path = std::wstring(appData) + L"\\" + STORE_DIR + L"\\" + STORE_FILE;
}
CoTaskMemFree(appData);
return path;
}
bool ParseUInt32(const std::string& s, uint32_t& out)
{
if (s.empty() || s.size() > 10 || !std::all_of(s.begin(), s.end(), [](unsigned char ch) { return std::isdigit(ch) != 0; }))
{
return false;
}
unsigned long long v = std::stoull(s);
if (v > 0xFFFFFFFFull)
{
return false;
}
out = static_cast<uint32_t>(v);
return true;
}
// Parses one [credential] block; returns false if a required field is missing or invalid
bool ParseCredential(const std::vector<std::pair<std::string, std::string>>& fields, StoredCredential& c)
{
bool ok = true;
for (const auto& [key, value] : fields)
{
if (key == "rpId")
{
c.rpId = value;
}
else if (key == "userName")
{
c.userName = value;
}
else if (key == "userId")
{
ok &= FromHex(value, c.userId);
}
else if (key == "credentialId")
{
ok &= FromHex(value, c.credentialId);
}
else if (key == "publicKey")
{
ok &= FromHex(value, c.publicKey);
}
else if (key == "aaguid")
{
ok &= FromHex(value, c.aaguid);
}
else if (key == "signCount")
{
ok &= ParseUInt32(value, c.signCount);
}
else if (key == "created")
{
c.created = value;
}
}
return ok && !c.rpId.empty() && !c.credentialId.empty() && !c.publicKey.empty();
}
}
CredentialStore::CredentialStore()
: m_path(StorePath())
{
Load();
}
std::string CredentialStore::Location() const
{
std::u8string s = fs::path(m_path).u8string();
return std::string(s.begin(), s.end());
}
size_t CredentialStore::Count() const
{
return m_credentials.size();
}
size_t CredentialStore::SkippedOnLoad() const
{
return m_skipped;
}
std::vector<StoredCredential> CredentialStore::FindByRpId(const std::string& rpId) const
{
std::vector<StoredCredential> result;
for (const auto& c : m_credentials)
{
if (c.rpId == rpId)
{
result.push_back(c);
}
}
return result;
}
std::optional<StoredCredential> CredentialStore::FindById(const std::vector<uint8_t>& credentialId) const
{
for (const auto& c : m_credentials)
{
if (c.credentialId == credentialId)
{
return c;
}
}
return std::nullopt;
}
bool CredentialStore::Add(const StoredCredential& cred)
{
auto it = std::find_if(m_credentials.begin(), m_credentials.end(),
[&](const StoredCredential& c) { return c.credentialId == cred.credentialId; });
if (it != m_credentials.end())
{
*it = cred;
}
else
{
m_credentials.push_back(cred);
}
return Save();
}
bool CredentialStore::UpdateSignCount(const std::vector<uint8_t>& credentialId, uint32_t signCount)
{
for (auto& c : m_credentials)
{
if (c.credentialId == credentialId)
{
c.signCount = signCount;
}
}
return Save();
}
bool CredentialStore::Remove(const std::vector<uint8_t>& credentialId)
{
m_credentials.erase(std::remove_if(m_credentials.begin(), m_credentials.end(),
[&](const StoredCredential& c) { return c.credentialId == credentialId; }), m_credentials.end());
return Save();
}
void CredentialStore::Load()
{
m_credentials.clear();
m_skipped = 0;
std::ifstream in{ fs::path(m_path) };
if (!in)
{
return; // nothing saved yet
}
std::vector<std::pair<std::string, std::string>> fields;
bool inBlock = false;
auto finishBlock = [&]()
{
if (inBlock)
{
StoredCredential c;
if (ParseCredential(fields, c))
{
m_credentials.push_back(std::move(c));
}
else
{
m_skipped++;
}
}
fields.clear();
};
std::string line;
while (std::getline(in, line))
{
if (line.rfind("\xEF\xBB\xBF", 0) == 0) // UTF-8 BOM, e.g. after editing with Notepad
{
line.erase(0, 3);
}
line = Trim(line);
if (line.empty() || line[0] == '#')
{
continue;
}
if (line == "[credential]")
{
finishBlock();
inBlock = true;
continue;
}
size_t eq = line.find('=');
if (inBlock && eq != std::string::npos)
{
fields.emplace_back(Trim(line.substr(0, eq)), Trim(line.substr(eq + 1)));
}
}
finishBlock();
}
bool CredentialStore::Save() const
{
if (m_path.empty())
{
return false;
}
// Write to a temporary file first and then replace the store, so a crash never leaves a half-written file
fs::path target(m_path);
fs::path temp = target;
temp += L".tmp";
std::error_code ec;
fs::create_directories(target.parent_path(), ec);
{
std::ofstream out(temp, std::ios::trunc); // text mode: "\n" is written as CRLF
if (!out)
{
return false;
}
out << FILE_HEADER;
for (const auto& c : m_credentials)
{
out << "\n[credential]\n"
<< "rpId = " << c.rpId << "\n"
<< "userName = " << c.userName << "\n"
<< "userId = " << Encoding::Hex(c.userId) << "\n"
<< "credentialId = " << Encoding::Hex(c.credentialId) << "\n"
<< "publicKey = " << Encoding::Hex(c.publicKey) << "\n"
<< "aaguid = " << Encoding::Hex(c.aaguid) << "\n"
<< "signCount = " << c.signCount << "\n"
<< "created = " << c.created << "\n";
}
if (!out.flush())
{
return false;
}
}
fs::rename(temp, target, ec); // replaces an existing file
return !ec;
}