/
secretstore.cpp
349 lines (303 loc) · 9.3 KB
/
secretstore.cpp
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/////////////////////////////////////////////////////////////////////////////
// Name: secretstore.cpp
// Purpose: wxWidgets sample showing the use of wxSecretStore class
// Author: Vadim Zeitlin
// Created: 2016-05-27
// Copyright: (c) 2016 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#include "wx/secretstore.h"
#include "wx/init.h"
#include "wx/crt.h"
#include "wx/log.h"
#include <cctype>
bool g_verbose_ {false};
bool g_binary_ {false};
bool Save(wxSecretStore& store, const wxString& service, const wxString& user)
{
char password[4096];
wxPrintf("Enter the password for %s/%s (echoing NOT disabled): ",
service, user);
if ( !wxFgets(password, WXSIZEOF(password), stdin) )
{
wxFprintf(stderr, "Password not stored.\n");
return false;
}
size_t size = wxStrlen(password);
if ( size )
{
// Strip trailing new line.
--size;
password[size] = 0;
}
wxSecretValue secret(size, password);
// The password data was copied into wxSecretValue, don't leave it lying
// around in the stack unnecessarily.
wxSecretValue::Wipe(size, password);
if ( !store.Save(service, user, secret) )
{
wxFprintf(stderr,
"Failed to save the password for %s/%s.\n",
service, user);
return false;
}
wxPrintf("Password for %s/%s saved.\n",
service, user);
return true;
}
bool Load(wxSecretStore& store, const wxString& service)
{
wxString user;
wxSecretValue secret;
if ( !store.Load(service, user, secret) )
{
wxFprintf(stderr, "Failed to load the password for %s.\n", service);
return false;
}
// Create a temporary variable just to make it possible to wipe it after
// using it.
wxString str(secret.GetAsString());
const size_t size = secret.GetSize();
wxPrintf("Password for %s/%s is %zu bytes long: \"%s\"\n",
service, user, size, str);
wxSecretValue::WipeString(str);
return true;
}
bool Delete(wxSecretStore& store, const wxString& service)
{
if ( !store.Delete(service) )
{
wxFprintf(stderr, "Password for %s not deleted.\n", service);
return false;
}
wxPrintf("Stored password for %s deleted.\n", service);
return true;
}
static bool PrintResult(bool ok)
{
wxPuts(ok ? "ok" : "ERROR");
return ok;
}
static void PrintSecrets(const wxSecretValue& expected, const wxSecretValue& loaded)
{
wxPrintf("Expected: size=%ld data=", expected.GetSize());
if (g_binary_)
{
wxPrintf("[");
size_t n = expected.GetSize();
const char* p = static_cast<const char *>(expected.GetData());
for (size_t i=0; i<n ;++i)
if (std::isprint(p[i]))
wxPrintf("%c", p[i]);
else
wxPrintf("\\x%hhx", p[i]);
wxPrintf("]\n");
}
else
{
wxPrintf("\"%s\"\n", expected.GetAsString());
}
wxPrintf("Loaded: size=%ld data=", loaded.GetSize());
if (g_binary_)
{
wxPrintf("[");
size_t n = loaded.GetSize();
const char* p = static_cast<const char *>(loaded.GetData());
for (size_t i=0; i<n ;++i)
if (std::isprint(p[i]))
wxPrintf("%c", p[i]);
else
wxPrintf("\\x%hhx", p[i]);
wxPrintf("]\n");
}
else
{
wxPrintf("\"%s\"\n", loaded.GetAsString());
}
}
bool SelfTest(wxSecretStore& store, const wxString& service)
{
wxPrintf("Running the tests with %s secrets...\n", g_binary_ ? "binary" : "text");
const wxString userTest("test");
const wxSecretValue secret1 = g_binary_ ? wxSecretValue(11, "secret\x1\x86\x2\x99\x3") : wxSecretValue("secret");
wxPrintf("Storing the password:\t");
bool ok = store.Save(service, userTest, secret1);
if ( !PrintResult(ok) )
{
// The rest of the tests will probably fail too, no need to continue.
wxPrintf("Bailing out.\n");
return false;
}
wxPrintf("Loading the password:\t");
wxSecretValue secret;
wxString user;
ok = PrintResult(store.Load(service, user, secret) &&
user == userTest &&
secret == secret1);
if (!ok && g_verbose_)
{
PrintSecrets(secret1, secret);
}
// Overwriting the password should work.
const wxSecretValue secret2 = g_binary_ ? wxSecretValue(11, "privet\x1\x86\x2\x99\x3") : wxSecretValue("privet");
wxPrintf("Changing the password:\t");
if ( PrintResult(store.Save(service, user, secret2)) )
{
wxPrintf("Reloading the password:\t");
if ( !PrintResult(store.Load(service, user, secret) &&
secret == secret2) )
{
ok = false;
if (g_verbose_)
{
PrintSecrets(secret2, secret);
}
}
}
else
ok = false;
wxPrintf("Deleting the password:\t");
if ( !PrintResult(store.Delete(service)) )
ok = false;
// This is supposed to fail now.
wxPrintf("Deleting it again:\t");
if ( !PrintResult(!store.Delete(service)) )
ok = false;
// And loading should fail too.
wxPrintf("Loading after deleting:\t");
if ( !PrintResult(!store.Load(service, user, secret)) )
ok = false;
if ( ok )
wxPrintf("All tests passed!\n");
return ok;
}
void usage(char **argv)
{
wxFprintf(stderr,
"Usage: %s save <service> <user>\n"
" or %s {load|delete} <service>\n"
" or %s [options] selftest <service>\n"
"\n"
"Sample showing wxSecretStore class functionality.\n"
"Specify one of the commands to perform the corresponding\n"
"function call. The \"service\" argument is mandatory for\n"
"all commands, \"save\" also requires \"user\" and will\n"
"prompt for password.\n\n"
"options:\n"
"\t-v\trun verbose (possibly shows secrets on errors)\n"
"\t-b\trun selftest using binary secrets (otherwise uses text strings)\n\n",
argv[0], argv[0], argv[0]);
}
int main(int argc, char **argv)
{
// To complement the standard EXIT_{SUCCESS,FAILURE}.
const int EXIT_SYNTAX = 2;
wxInitializer initializer;
if ( !initializer )
{
fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
return EXIT_FAILURE;
}
wxString operation;
wxString service;
wxString user;
for (int arg=1; arg<argc ;++arg)
{
if (argv[arg][0] == '-')
{
if (argv[arg][1] == 'v' && argv[arg][2] == '\0')
g_verbose_ = true;
else if (argv[arg][1] == 'b' && argv[arg][2] == '\0')
g_binary_ = true;
else
{
wxFprintf(stderr,
"ERROR: Unknown switch : %s\n\n",
argv[arg]);
usage(argv);
return EXIT_SYNTAX;
}
}
else if (operation.IsEmpty())
{
operation = argv[arg];
}
else if (service.IsEmpty())
{
service = argv[arg];
}
else if (operation == "save" && user.IsEmpty())
{
user = argv[arg];
}
else
{
wxFprintf(stderr,
"ERROR: Unknown argument : %s\n\n",
argv[arg]);
usage(argv);
return EXIT_SYNTAX;
}
}
if (operation.IsEmpty() || service.IsEmpty())
{
if (operation.IsEmpty())
wxFprintf(stderr,
"ERROR: Missing required operation argument\n\n");
else
wxFprintf(stderr,
"ERROR: Missing required service argument\n\n");
usage(argv);
return EXIT_SYNTAX;
}
wxSecretStore store = wxSecretStore::GetDefault();
wxString errmsg;
if ( !store.IsOk(&errmsg) )
{
wxFprintf(stderr, "Failed to create default secret store (%s)\n",
errmsg);
return EXIT_FAILURE;
}
bool ok;
if ( operation == "save" )
{
if (user.IsEmpty())
{
wxFprintf(stderr,
"ERROR: Missing required user argument\n\n");
usage(argv);
return EXIT_SYNTAX;
}
ok = Save(store, service, user);
}
else if ( operation == "load" )
{
ok = Load(store, service);
}
else if ( operation == "delete" )
{
ok = Delete(store, service);
}
else if ( operation == "selftest" )
{
ok = SelfTest(store, service);
}
else
{
wxFprintf(stderr,
"Unknown operation \"%s\", expected \"save\", \"load\" or "
"\"delete\".\n\n",
operation);
usage(argv);
return EXIT_SYNTAX;
}
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}