-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
credentials.cpp
354 lines (322 loc) · 10.3 KB
/
credentials.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
350
351
352
353
354
/*
Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "credentials.hpp"
#include "preferences/general.hpp"
#include "serialization/unicode.hpp"
#include "filesystem.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include <algorithm>
#include <memory>
#ifndef __APPLE__
#include <openssl/rc4.h>
#else
#include <CommonCrypto/CommonCryptor.h>
#endif
#ifdef _WIN32
#include <boost/range/iterator_range.hpp>
#include <windows.h>
#endif
static lg::log_domain log_config("config");
#define ERR_CFG LOG_STREAM(err , log_config)
class secure_buffer : public std::vector<unsigned char>
{
public:
template<typename... T> secure_buffer(T&&... args)
: vector<unsigned char>(std::forward<T>(args)...)
{}
~secure_buffer()
{
std::fill(begin(), end(), '\0');
}
};
struct login_info
{
std::string username, server;
secure_buffer key;
login_info(const std::string& username, const std::string& server, const secure_buffer& key)
: username(username), server(server), key(key)
{}
login_info(const std::string& username, const std::string& server)
: username(username), server(server), key()
{}
std::size_t size() const
{
return 3 + username.size() + server.size() + key.size();
}
};
static std::vector<login_info> credentials;
// Separate password entries with formfeed
static const unsigned char CREDENTIAL_SEPARATOR = '\f';
static secure_buffer encrypt(const secure_buffer& text, const secure_buffer& key);
static secure_buffer decrypt(const secure_buffer& text, const secure_buffer& key);
static secure_buffer build_key(const std::string& server, const std::string& login);
static secure_buffer escape(const secure_buffer& text);
static secure_buffer unescape(const secure_buffer& text);
static std::string get_system_username()
{
std::string res;
#ifdef _WIN32
wchar_t buffer[300];
DWORD size = 300;
if(GetUserNameW(buffer, &size)) {
//size includes a terminating null character.
assert(size > 0);
res = unicode_cast<std::string>(boost::iterator_range<wchar_t*>(buffer, buffer + size - 1));
}
#else
if(char* const login = getenv("USER")) {
res = login;
}
#endif
return res;
}
static void clear_credentials()
{
// Zero them before clearing.
// Probably overly paranoid, but doesn't hurt?
for(auto& cred : credentials) {
std::fill(cred.username.begin(), cred.username.end(), '\0');
std::fill(cred.server.begin(), cred.server.end(), '\0');
}
credentials.clear();
}
static const std::string EMPTY_LOGIN = "@@";
namespace preferences
{
std::string login()
{
std::string name = preferences::get("login", EMPTY_LOGIN);
if(name == EMPTY_LOGIN) {
name = get_system_username();
} else if(name.size() > 2 && name.front() == '@' && name.back() == '@') {
name = name.substr(1, name.size() - 2);
} else {
ERR_CFG << "malformed user credentials (did you manually edit the preferences file?)" << std::endl;
}
if(name.empty()) {
return "player";
}
return name;
}
void set_login(const std::string& login)
{
preferences::set("login", '@' + login + '@');
}
bool remember_password()
{
return preferences::get("remember_password", false);
}
void set_remember_password(bool remember)
{
preferences::set("remember_password", remember);
if(remember) {
load_credentials();
} else {
clear_credentials();
}
}
std::string password(const std::string& server, const std::string& login)
{
if(!remember_password()) {
if(!credentials.empty() && credentials[0].username == login && credentials[0].server == server) {
auto temp = decrypt(credentials[0].key, build_key(server, login));
return std::string(temp.begin(), temp.end());
} else {
return "";
}
}
auto cred = std::find_if(credentials.begin(), credentials.end(), [&](const login_info& cred) {
return cred.server == server && cred.username == login;
});
if(cred == credentials.end()) {
return "";
}
auto temp = decrypt(cred->key, build_key(server, login));
return std::string(temp.begin(), temp.end());
}
void set_password(const std::string& server, const std::string& login, const std::string& key)
{
secure_buffer temp(key.begin(), key.end());
if(!remember_password()) {
clear_credentials();
credentials.emplace_back(login, server, encrypt(temp, build_key(server, login)));
return;
}
auto cred = std::find_if(credentials.begin(), credentials.end(), [&](const login_info& cred) {
return cred.server == server && cred.username == login;
});
if(cred == credentials.end()) {
// This is equivalent to emplace_back, but also returns the iterator to the new element
cred = credentials.emplace(credentials.end(), login, server);
}
cred->key = encrypt(temp, build_key(server, login));
}
void load_credentials()
{
if(!remember_password()) {
return;
}
clear_credentials();
std::string cred_file = filesystem::get_credentials_file();
if(!filesystem::file_exists(cred_file)) {
return;
}
filesystem::scoped_istream stream = filesystem::istream_file(cred_file, false);
// Credentials file is a binary blob, so use streambuf iterator
secure_buffer data((std::istreambuf_iterator<char>(*stream)), (std::istreambuf_iterator<char>()));
data = decrypt(data, build_key("global", get_system_username()));
if(data.empty() || data[0] != CREDENTIAL_SEPARATOR) {
ERR_CFG << "Invalid data in credentials file\n";
return;
}
for(const std::string& elem : utils::split(std::string(data.begin(), data.end()), CREDENTIAL_SEPARATOR, utils::REMOVE_EMPTY)) {
std::size_t at = elem.find_last_of('@');
std::size_t eq = elem.find_first_of('=', at + 1);
if(at != std::string::npos && eq != std::string::npos) {
secure_buffer key(elem.begin() + eq + 1, elem.end());
credentials.emplace_back(elem.substr(0, at), elem.substr(at + 1, eq - at - 1), unescape(key));
}
}
}
void save_credentials()
{
if(!remember_password()) {
filesystem::delete_file(filesystem::get_credentials_file());
return;
}
secure_buffer credentials_data(1, CREDENTIAL_SEPARATOR);
std::size_t offset = 1;
for(const auto& cred : credentials) {
credentials_data.resize(credentials_data.size() + cred.size(), CREDENTIAL_SEPARATOR);
std::copy(cred.username.begin(), cred.username.end(), credentials_data.begin() + offset);
offset += cred.username.size();
credentials_data[offset++] = '@';
std::copy(cred.server.begin(), cred.server.end(), credentials_data.begin() + offset);
offset += cred.server.size();
credentials_data[offset++] = '=';
secure_buffer key_escaped = escape(cred.key);
// Escaping may increase the length, so resize again if so
credentials_data.resize(credentials_data.size() + key_escaped.size() - cred.key.size());
std::copy(key_escaped.begin(), key_escaped.end(), credentials_data.begin() + offset);
offset += key_escaped.size() + 1;
}
try {
filesystem::scoped_ostream credentials_file = filesystem::ostream_file(filesystem::get_credentials_file());
secure_buffer encrypted = encrypt(credentials_data, build_key("global", get_system_username()));
credentials_file->write(reinterpret_cast<const char*>(encrypted.data()), encrypted.size());
} catch(const filesystem::io_exception&) {
ERR_CFG << "error writing to credentials file '" << filesystem::get_credentials_file() << "'" << std::endl;
}
}
}
// TODO: Key-stretching (bcrypt was recommended)
secure_buffer build_key(const std::string& server, const std::string& login)
{
std::string sysname = get_system_username();
secure_buffer result(std::max<std::size_t>(server.size() + login.size() + sysname.size(), 32));
unsigned char i = 0;
std::generate(result.begin(), result.end(), [&i]() {return 'x' ^ i++;});
std::copy(login.begin(), login.end(), result.begin());
std::copy(sysname.begin(), sysname.end(), result.begin() + login.size());
std::copy(server.begin(), server.end(), result.begin() + login.size() + sysname.size());
return result;
}
static secure_buffer rc4_crypt(const secure_buffer& text, const secure_buffer& key)
{
secure_buffer result(text.size(), '\0');
#ifndef __APPLE__
RC4_KEY cipher_key;
RC4_set_key(&cipher_key, key.size(), key.data());
const std::size_t block_size = key.size();
const std::size_t blocks = text.size() / block_size;
const std::size_t extra = text.size() % block_size;
for(std::size_t i = 0; i < blocks * block_size; i += block_size) {
RC4(&cipher_key, block_size, text.data() + i, result.data() + i);
}
if(extra) {
std::size_t i = blocks * block_size;
RC4(&cipher_key, extra, text.data() + i, result.data() + i);
}
#else
size_t outWritten = 0;
CCCryptorStatus ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmRC4,
kCCOptionPKCS7Padding,
key.data(),
key.size(),
nullptr,
text.data(),
text.size(),
result.data(),
result.size(),
&outWritten);
assert(ccStatus == kCCSuccess);
assert(outWritten == text.size());
#endif
return result;
}
secure_buffer encrypt(const secure_buffer& text, const secure_buffer& key)
{
return rc4_crypt(text, key);
}
secure_buffer decrypt(const secure_buffer& text, const secure_buffer& key)
{
auto buf = rc4_crypt(text, key);
while(!buf.empty() && buf.back() == 0) {
buf.pop_back();
}
return buf;
}
secure_buffer unescape(const secure_buffer& text)
{
secure_buffer unescaped;
unescaped.reserve(text.size());
bool escaping = false;
for(char c : text) {
if(escaping) {
if(c == '\xa') {
unescaped.push_back('\xc');
} else if(c == '.') {
unescaped.push_back('@');
} else {
unescaped.push_back(c);
}
escaping = false;
} else if(c == '\x1') {
escaping = true;
} else {
unescaped.push_back(c);
}
}
assert(!escaping);
return unescaped;
}
secure_buffer escape(const secure_buffer& text)
{
secure_buffer escaped;
escaped.reserve(text.size());
for(char c : text) {
if(c == '\x1') {
escaped.push_back('\x1');
escaped.push_back('\x1');
} else if(c == '\xc') {
escaped.push_back('\x1');
escaped.push_back('\xa');
} else if(c == '@') {
escaped.push_back('\x1');
escaped.push_back('.');
} else {
escaped.push_back(c);
}
}
return escaped;
}