-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceb2sqlgz.cpp
267 lines (234 loc) · 7.85 KB
/
ceb2sqlgz.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
/*
Copyright © 2022 mjk <https://github.com/Yuubi-san>.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 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; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>.
Additional permission under GNU AGPL version 3 section 7
If you modify this Program, or any covered work, by linking or combining
it with OpenSSL project's OpenSSL library (or a modified version of that
library), containing parts covered by the terms of the OpenSSL or SSLeay
licenses, the licensors of this Program grant you additional permission
to convey the resulting work. Corresponding Source for a non-source form
of such a combination shall include the source code for the parts of OpenSSL
used as well as that of the covered work.
*/
#include <limits>
#include <chrono>
#include <uchar.h>
#include <clocale>
#include <codecvt>
#include <fstream>
#include <iostream>
#include <termios.h>
#include <openssl/evp.h>
#include "unaesgcm/unaesgcm.hpp"
#if __cpp_lib_char8_t
using std::u8string;
using std::u8string_view;
#else
using u8string = std::basic_string <unsigned char>;
using u8string_view = std::basic_string_view<u8string::value_type>;
#endif
template<typename> struct smaller;
template<> struct smaller<std::uint16_t> { using type = std::uint8_t; };
template<> struct smaller<std::uint32_t> { using type = std::uint16_t; };
template<> struct smaller<std::uint64_t> { using type = std::uint32_t; };
template<typename T>
static auto readi( std::istream &in )
{
using S = typename smaller<T>::type;
const auto
hi = readi<S>(in),
lo = readi<S>(in);
return static_cast<T>( T{hi} << std::numeric_limits<S>::digits | lo );
}
template<typename T>
static auto read( std::istream &in )
{
static_assert( std::is_standard_layout_v<T> and std::is_trivial_v<T> );
T pod;
in.read( reinterpret_cast<char *>(&pod), sizeof pod );
return pod;
}
template<>
auto readi<std::uint8_t>( std::istream &in )
{
return read<std::uint8_t>( in );
}
template<>
auto read<u8string>( std::istream &in )
{
u8string s( readi<std::uint16_t>(in), u8'\0' );
in.read( reinterpret_cast<char *>(data(s)), static_cast<long>(size(s)) );
return s;
}
static auto readv( std::istream &in, const std::size_t sz )
{
std::vector<byte> v( sz );
in.read( reinterpret_cast<char *>(data(v)), static_cast<long>(size(v)) );
return v;
}
static auto put_sys_time(
const std::chrono::system_clock::time_point t, const char *const fmt )
{
const auto tt = std::chrono::system_clock::to_time_t( t );
return std::put_time( std::localtime(&tt), fmt );
}
static auto derive_key( const u8string_view passphrase,
const std::array<byte,bits<128>> salt )
{
constexpr auto max = unsigned{std::numeric_limits<int>::max()};
if ( size(passphrase) > max )
throw std::length_error{"passphrase too long"};
constexpr auto rounds = 1024;
std::array<byte,bits<128>> key;
if ( PKCS5_PBKDF2_HMAC_SHA1(
reinterpret_cast<const char *>(data(passphrase)),
static_cast<int>(size(passphrase)),
data(salt), size(salt),
rounds,
size(key), data(key)) )
return key;
throw std::runtime_error{"PKCS5_PBKDF2_HMAC_SHA1 failed"};
}
static u8string utf_this_utffing_shit( const std::string_view str )
// aka global_locale_encoding_to_utf8
{
std::wstring_convert<std::codecvt_utf8<char32_t>,char32_t> wc;
u8string result;
auto in_pos = data(str);
const auto ed = in_pos + size(str);
std::mbstate_t state{};
while ( in_pos != ed )
{
char32_t cp;
switch ( const auto n =
::mbrtoc32(&cp, in_pos, static_cast<std::size_t>(ed - in_pos), &state) )
{
case 0:
throw std::invalid_argument{"null character in passphrase"};
case std::size_t{0}-1:
throw std::invalid_argument{"illegal byte sequence in passphrase"};
case std::size_t{0}-2:
throw std::invalid_argument{"incomplet byte sequence in passphrase"};
case std::size_t{0}-3:
#if __cpp_lib_unreachable
std::unreachable();
#else
__builtin_unreachable();
#endif
default:
const auto res = wc.to_bytes(cp);
result.append(
reinterpret_cast<const u8string::value_type *>(data(res)),
size(res) );
in_pos += n;
}
}
return result;
}
int main( const int argc, const char *const *const argv ) try
{
using ms = std::chrono::milliseconds;
if ( argc != 2 )
{
std::clog <<"usage: "<< (argc ? argv[0] : "ceb2sqlgz") <<" file.ceb\n";
return 1;
}
std::cin.exceptions( std::ios_base::badbit|std::ios_base::failbit );
std::string passphrase;
{
constexpr auto stdin_fd = 0;
struct restorer { std::optional<::termios> otios; ~restorer() {
if ( otios and ::tcsetattr(stdin_fd, TCSANOW, &*otios) )
{
const auto e = errno;
std::clog << "error: couldn't re-enable password echoing: "
"tcsetattr resulted in errno "<< e <<'\n';
}
} } r;
int err = 0;
if ( ::termios tios; ::tcgetattr(stdin_fd, &tios) == 0 )
{
r.otios = tios;
tios.c_lflag &= ~decltype(tios.c_lflag){ECHO};
if ( ::tcsetattr(stdin_fd, TCSANOW, &tios) )
{
err = errno;
std::clog << "warning: couldn't disable password echoing: "
"tcsetattr resulted in errno "<< err <<'\n';
r.otios = {};
}
}
else
{
err = errno;
if ( err != ENOTTY )
std::clog << "warning: can't disable password echoing: "
"tcgetattr resulted in errno "<< err <<'\n';
}
if ( err == 0 )
std::cerr << "Passphrase plz: ";
else if ( err != ENOTTY )
std::cerr << "Passphrase plz (will be echoed): ";
getline( std::cin, passphrase );
if ( err == 0 )
std::cerr << "\nk thx\n";
else if ( err != ENOTTY )
std::cerr << "k thx\n";
}
std::setlocale(LC_ALL, "");
const auto u8passphrase = utf_this_utffing_shit( passphrase );
std::setlocale(LC_ALL, "C");
std::ifstream in{ argv[1], std::ios_base::binary };
auto &out = std::cout;
in.exceptions(
std::ios_base::badbit|std::ios_base::failbit|std::ios_base::eofbit );
const auto version = readi<std::uint32_t>(in);
if ( version != 1 )
{
std::clog << argv[0] <<
": error: unsupported CEB version ("<< version <<") or not a CEB file\n";
return 1;
}
const auto producer = read<u8string>(in);
const auto account = read<u8string>(in);
const auto time =
std::chrono::system_clock::time_point{ms{readi<std::uint64_t>(in)}};
const auto iv = readv(in, bits<96>);
const auto salt = read<std::array<byte,bits<128>>>(in);
std::clog
<< argv[0] <<": format: CEB v"<< version <<'\n'
<< argv[0] <<": producer: "<<
reinterpret_cast<const char *>(producer.c_str()) <<'\n'
<< argv[0] <<": account: "<<
reinterpret_cast<const char *>(account.c_str()) <<'\n'
<< argv[0] <<": datetime: "<<
put_sys_time(time, "%F %T") <<'.'<<
std::setw(3) << std::setfill('0') <<
time.time_since_epoch() / ms{1} % 1000 <<
put_sys_time(time, " %z") <<'\n'
;
dump_hex(std::clog << argv[0] <<": IV: ", iv) << '\n';
dump_hex(std::clog << argv[0] <<": salt: ", salt) << '\n';
if ( unaesgcm(iv, derive_key(u8passphrase, salt), in, out) )
return 0;
else
{
std::clog
<< argv[0] <<": authentication failed (wrong password? corrupt file?)\n";
return 1;
}
}
catch ( ... )
{
// Catching only for the sake of unwinding.
// Let the implementation do the error printing:
throw;
}