-
Notifications
You must be signed in to change notification settings - Fork 25
/
gwsettings.cc
1412 lines (1142 loc) · 34.4 KB
/
gwsettings.cc
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* bcm2-utils
* Copyright (C) 2016 Joseph Lehner <[email protected]>
*
* bcm2-utils 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 3 of the License, or
* (at your option) any later version.
*
* bcm2-utils 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bcm2-utils. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <boost/crc.hpp>
#include <algorithm>
#include "nonvoldef.h"
#include "gwsettings.h"
#include "crypto.h"
using namespace std;
using namespace bcm2dump;
using namespace bcm2utils;
namespace bcm2cfg {
namespace {
string read_stream(istream& is)
{
return string(std::istreambuf_iterator<char>(is), {});
}
string gws_checksum(string buf, const csp<profile>& p)
{
return hash_md5(buf + (p ? p->md5_key() : ""));
}
unsigned log2(unsigned num)
{
unsigned ret = 0;
while (num >>= 1) {
++ret;
}
return ret;
}
unsigned pow2(unsigned num)
{
unsigned ret = 2;
while (num--) {
ret <<= 1;
}
return ret;
}
string gws_crypt(const string& buf, const string& key, int type, bool encrypt)
{
if (type == BCM2_CFG_ENC_AES256_ECB) {
return crypt_aes_256_ecb(buf, key, encrypt);
} else if (type == BCM2_CFG_ENC_AES128_CBC) {
return crypt_aes_128_cbc(buf, key, encrypt);
} else if (type == BCM2_CFG_ENC_3DES_ECB) {
return crypt_3des_ecb(buf, key, encrypt);
} else if (type == BCM2_CFG_ENC_DES_ECB) {
return crypt_des_ecb(buf, key, encrypt);
} else if (type == BCM2_CFG_ENC_SUB_16x16) {
return crypt_sub_16x16(buf, encrypt);
} else if (type == BCM2_CFG_ENC_XOR) {
return crypt_xor_char(buf, key);
} else {
throw runtime_error("invalid encryption type " + to_string(type));
}
}
unsigned gws_enc_blksize(const csp<profile>& p)
{
switch (p->cfg_encryption()) {
case BCM2_CFG_ENC_AES256_ECB:
case BCM2_CFG_ENC_AES128_CBC:
case BCM2_CFG_ENC_SUB_16x16:
return 16;
case BCM2_CFG_ENC_DES_ECB:
case BCM2_CFG_ENC_3DES_ECB:
return 8;
default:
return 1;
}
}
bool gws_unpad(string& buf, const csp<profile>& p)
{
int pad = p->cfg_padding();
unsigned blksize = gws_enc_blksize(p);
bool pad_optional = p->cfg_flags() & BCM2_CFG_FMT_GWS_PAD_OPTIONAL;
if (pad == BCM2_CFG_PAD_PKCS7 || pad == BCM2_CFG_PAD_ANSI_X9_23 || pad == BCM2_CFG_PAD_ANSI_ISH) {
unsigned padnum = buf.back() + (pad == BCM2_CFG_PAD_ANSI_ISH ? 1 : 0);
// add 16 to buf size to account for the checksum
unsigned expected = blksize - (((buf.size() + 16) - padnum) % blksize);
if (padnum == expected || (expected == 0 && padnum == blksize && !pad_optional)) {
logger::d() << "padding=" << to_hex(buf.substr(buf.size() - padnum)) << endl;
buf.resize(buf.size() - padnum);
return true;
}
} else if (pad == BCM2_CFG_PAD_ZERO) {
bool ok = true;
for (char c : buf.substr(align_left(buf.size(), blksize), buf.size() % blksize)) {
if (c) {
ok = false;
break;
}
}
if (ok) {
buf.resize(align_left(buf.size(), blksize));
return true;
}
} else if (pad == BCM2_CFG_PAD_ZEROBLK) {
string blk(blksize, 0x00);
if (buf.substr(buf.size() - blksize) == blk) {
buf.resize(buf.size() - blksize);
return true;
}
return false;
} else {
return false;
}
logger::v() << "failed to remove padding" << endl;
return false;
}
string gws_decrypt(string buf, string& checksum, string& key, const csp<profile>& p, bool& padded)
{
int flags = p->cfg_flags();
int enc = p->cfg_encryption();
logger::d() << "decrypting with profile " << p->name() << endl;
if (flags & BCM2_CFG_FMT_GWS_LEN_PREFIX) {
auto len = be_to_h(extract<uint32_t>(checksum));
if (len == (buf.size() + 12)) {
checksum.erase(0, 4);
checksum.append(buf.substr(0, 4));
buf.erase(0, 4);
} else {
logger::d() << "unexpected length prefix: " << len << endl;
}
} else if (flags & BCM2_CFG_FMT_GWS_CLEN_PREFIX) {
if (checksum == "Content-Length: ") {
auto pos = buf.find("\r\n\r\n");
auto len = lexical_cast<uint32_t>(buf.substr(0, pos));
auto beg = pos + 4;
if (len != (buf.size() - beg)) {
logger::d() << "unexpected length prefix: " << len << endl;
}
checksum = buf.substr(beg, 16);
buf = buf.substr(beg + 16);
} else {
logger::d() << "length prefix is missing" << endl;
}
}
if (flags & BCM2_CFG_FMT_GWS_FULL_ENC) {
buf = checksum + buf;
}
if (enc == BCM2_CFG_ENC_MOTOROLA) {
if (key.empty()) {
key = buf.back();
}
buf = crypt_motorola(buf.substr(0, buf.size() - 1), key);
} else {
buf = gws_crypt(buf, key, enc, false);
}
padded = gws_unpad(buf, p);
if (flags & BCM2_CFG_FMT_GWS_FULL_ENC) {
checksum = buf.substr(0, 16);
buf = buf.substr(16);
}
return buf;
}
string gws_encrypt(string buf, const string& key, const csp<profile>& p, bool pad)
{
int flags = p->cfg_flags();
int enc = p->cfg_encryption();
if (flags & BCM2_CFG_FMT_GWS_FULL_ENC) {
buf = gws_checksum(buf, p) + buf;
}
// TODO move all padding stuff to crypto.cc
if (!(flags & BCM2_CFG_FMT_GWS_PAD_OPTIONAL) && !pad) {
pad = true;
logger::d() << "force-enabling padding" << endl;
}
if (enc == BCM2_CFG_ENC_MOTOROLA) {
return crypt_motorola(buf, key) + key;
} else if (enc != BCM2_CFG_ENC_NONE) {
if (pad) {
int padding = p->cfg_padding();
if (enc == BCM2_CFG_ENC_SUB_16x16) {
if (buf.size() & 1) {
buf += '\x00';
}
} else if (padding == BCM2_CFG_PAD_ZEROBLK) {
buf += string(16, '\0');
} else {
unsigned blksize = gws_enc_blksize(p);
unsigned padnum = blksize - (buf.size() % blksize);
if (padding == BCM2_CFG_PAD_ANSI_X9_23) {
buf += string(padnum - 1, '\0');
buf += char(padnum & 0xff);
} else if (padding == BCM2_CFG_PAD_ANSI_ISH) {
buf += string(padnum - 1, '\0');
buf += char((padnum - 1) & 0xff);
} else if (padding == BCM2_CFG_PAD_PKCS7) {
buf += string(padnum, char(padnum & 0xff));
} else if (padding == BCM2_CFG_PAD_ZERO) {
buf += string(padnum, '\0');
} else if (padding) {
throw runtime_error("unknown padding type");
}
}
}
buf = gws_crypt(buf, key, enc, true);
} else {
throw user_error("profile " + p->name() + " does not support encryption");
}
if (!(flags & BCM2_CFG_FMT_GWS_FULL_ENC)) {
buf = gws_checksum(buf, p) + buf;
}
if (flags & BCM2_CFG_FMT_GWS_LEN_PREFIX) {
buf.insert(0, to_buf(h_to_be(buf.size())));
} else if (flags & BCM2_CFG_FMT_GWS_CLEN_PREFIX) {
buf.insert(0, "Content-Length: " + to_string(buf.size()) + "\r\n\r\n");
}
return buf;
}
string group_header_to_string(int format, const string& checksum, bool is_chksum_valid,
size_t size, bool is_size_valid, const string& key, bool is_encrypted,
const string& profile, bool is_auto_profile, const string& circumfix)
{
ostringstream ostr;
ostr << "type : ";
switch (format) {
case nv_group::fmt_gws:
ostr << "gwsettings";
break;
case nv_group::fmt_gwsdyn:
ostr << "gwsdyn";
break;
case nv_group::fmt_dyn:
ostr << "dyn";
break;
case nv_group::fmt_perm:
ostr << "perm";
break;
case nv_group::fmt_boltenv:
ostr << "boltenv";
break;
default:
ostr << "(unknown)";
}
ostr << endl << "profile : ";
if (profile.empty()) {
ostr << "(unknown)" << endl;
} else {
ostr << profile << (is_auto_profile ? "" : " (forced)") << endl;
}
ostr << "checksum: " << checksum;
if (!profile.empty() || format != nv_group::fmt_gws) {
ostr << " " << (is_chksum_valid ? "(ok)" : "(bad)") << endl;
} else {
ostr << endl;
}
ostr << "size : ";
if (!is_encrypted || !key.empty()) {
ostr << size << " " << (is_size_valid ? "(ok)" : "(bad)") << endl;
} else {
ostr << size << " (unknown)" << endl;
}
if (is_encrypted) {
ostr << "key : " << (key.empty() ? "(unknown)" : to_hex(key)) << endl;
}
if (!circumfix.empty()) {
ostr << "circfix : " << to_hex(circumfix) << endl;
}
return ostr.str();
}
class permdyn : public encryptable_settings
{
public:
permdyn(int format, const csp<bcm2dump::profile>& p, const string& key)
: encryptable_settings("permdyn", format, p), m_key(key) {}
virtual size_t bytes() const override
{ return m_size.num(); }
virtual size_t data_bytes() const override
{ return bytes() - 8; }
virtual bool is_valid() const override
{ return m_magic_valid; }
virtual istream& read(istream& is) override
{
// TODO move this to settings::read()
is.seekg(-16, ios::cur);
auto beg = is.tellg();
m_magic_valid = false;
if (!m_size.read(is) || !m_checksum.read(is)) {
throw runtime_error("failed to read header");
}
if (m_size.num() == 0xffffffff && m_checksum.num() == 0xffffffff) {
// probably an old-style permnv/dynnv file, which starts
// with a prefix of 202 0xff bytes (of which we've already read 8).
string prefix(202 - 8, '\0');
if (!is.read(&prefix[0], prefix.size())) {
throw runtime_error("failed to read prefix");
}
if (prefix.find_first_not_of('\xff') != string::npos) {
return is;
}
m_magic_valid = true;
m_old_style = true;
// seek to the footer
is.seekg(-8, ios::end);
m_raw_size = is.tellg();
uint32_t segment_size;
uint32_t segment_bitmask;
nv_u32::read(is, segment_size);
nv_u32::read(is, segment_bitmask);
unsigned segment_index = -int32_t(segment_bitmask);
streampos offset = 0;
if (segment_size > m_raw_size || segment_size == 0xffffffff) {
logger::w() << "invalid segment size: 0x" << to_hex(segment_size) << endl;
} else {
m_write_count = log2(segment_index) - 1;
if (pow2(m_write_count) != segment_index) {
logger::w() << "invalid segment bitmask: 0x" << to_hex(segment_bitmask) << endl;
m_write_count = 0;
} else {
offset = segment_size * min(m_write_count, 16u);
logger::d() << "write count: " << m_write_count << ", offset: " << offset << endl;
}
}
if ((beg + offset) >= m_raw_size) {
logger::w() << "segment offset " << offset << " exceeds maximum size " << m_raw_size << endl;
offset = 0;
}
// seek to the beginning of the settings group data
is.seekg(beg + offset + streampos(202));
for (int i = 0; i < 2; ++i) {
// re-read the checksum and size fields
if (!m_size.read(is) || !m_checksum.read(is)) {
throw runtime_error("failed to read header");
}
if (!i && (m_size.num() == 0xffffffff || m_size.num() > m_raw_size)) {
// at least try to read the first copy, if we've messed up the calculations above
logger::w() << "invalid data size " << m_size.num() << "; retrying at offset 0" << endl;
is.seekg(beg + streampos(202));
} else {
break;
}
}
// TODO try reading from the backup
} else {
m_old_style = false;
// FIXME
m_magic_valid = true;
}
string buf = read_stream(is);
if (buf.size() < (m_size.num() - 8)) {
logger::w() << type() << ": read " << buf.size() << "b, expected at least " << m_size.num() - 8 << endl;
m_size_valid = false;
} else {
buf.resize(m_size.num() - 8);
m_size_valid = true;
}
// minus 8, since m_size includes itself (4 bytes) plus the checksum (also 4 bytes)
uint32_t checksum = calc_checksum(buf.substr(0, m_size.num() - 8));
m_checksum_valid = checksum == m_checksum.num();
if (!m_checksum_valid) {
logger::d() << type() << ": checksum mismatch: " << to_hex(checksum) << " / " << to_hex(m_checksum.num()) << endl;
}
istringstream istr(buf);
settings::read(istr);
if (!key().empty()) {
// a key was specified, but we must check if the file is actually encrypted. compared with
// the GatewaySettings there's no easy way to do this, as there's no magic we can check for.
//
// we thus parse the data twice, once as-is, and once decrypted with the supplied key,
// and check which yields more settings groups. if there's only one group in both cases,
// do a sanity check on the version
auto unenc_groups = parts();
m_parts.clear();
istr.str(crypt_aes_256_ecb(istr.str(), key(), false));
settings::read(istr);
if (unenc_groups.size() > parts().size()) {
// more groups when not decrypted -> file isn't encrypted
m_key.clear();
} else if (unenc_groups.size() == m_parts.size() && m_parts.size() == 1) {
// one part in both cases
auto unenc_ver = nv_val_cast<nv_group>(unenc_groups[0].val)->version();
auto enc_ver = nv_val_cast<nv_group>(parts()[0].val)->version();
if (enc_ver.major() > 5 || enc_ver.minor() > 100) {
// assume that file isn't encrypted, as major versions
// are usually 0 or 1. the limit on the minor version
// should be good for most cases too.
m_key.clear();
}
}
if (m_key.empty()) {
groups(unenc_groups);
}
}
return is;
}
virtual ostream& write(ostream& os) const override
{
ostringstream ostr;
settings::write(ostr);
string buf = ostr.str();
if (!key().empty()) {
buf = crypt_aes_256_ecb(buf, key(), true);
}
ostr.str("");
if (m_old_style) {
ostr << string(202, '\xff');
}
nv_u32::write(ostr, 8 + buf.size());
nv_u32::write(ostr, calc_checksum(buf));
ostr.write(buf.data(), buf.size());
os << ostr.str();
if (m_old_style) {
// currently, this function simply writes a file that pretends to be
// a) a nonvol file that has been written just once
// b) uses the same data for both the primary and backup
//
// TODO actually append our data
// set offset of the backup data to the end of the primary data
size_t segment_size = ostr.tellp();
// some firmwares will not consider a file valid unless each segment
// takes up at least 1/16th of the partition.
segment_size = max(segment_size, (m_raw_size + 8u) / 16lu);
// 8 bytes for the footer are already subtracted
ssize_t diff = m_raw_size - segment_size;
// we've written the primary data, but still need to write the backup data.
// first, check if it would even fit!
if (segment_size < diff) {
// backup data actually fits. we could probably just use `segment_size`, but
// all firmwares seem to include at least some padding in between, and
// some of those align the offset to 0x1000 (or 0x100?).
if (align_left(segment_size, 0x1000) < diff) {
segment_size = align_right(segment_size, 0x1000);
} else if (align_left(segment_size, 0x100) < diff) {
segment_size = align_right(segment_size, 0x100);
}
diff = m_raw_size - segment_size;
// write padding between segments
os << string(segment_size - ostr.tellp(), '\xff');
// write backup segment
os << ostr.str();
diff -= ostr.tellp();
} else {
logger::i() << "no space to fit backup data" << endl;
}
if (diff < 0) {
throw runtime_error("file size exceeds maximum of " + ::to_string(m_raw_size));
}
// pad to size
os << string(diff, '\xff');
nv_u32::write(os, segment_size);
// pretend that this is a file that has been written once
nv_u32::write(os, 0xfffffffc);
}
if (!os) {
throw runtime_error("write error");
}
return os;
}
virtual string header_to_string() const override
{
return group_header_to_string(m_format, to_hex(m_checksum.num()), m_checksum_valid,
m_size.num(), m_size_valid, "", false, "", false, "");
}
virtual bool padded() const override
{ return false; }
virtual void padded(bool) override
{}
virtual string key() const override
{ return m_key; }
virtual void key(const string& key) override
{ m_key = key; }
private:
static uint32_t calc_checksum(const string& buf)
{
uint32_t remaining = buf.size();
// the checksum is calculated from the header (u32 size, u32 checksum), with
// the checksum part set to 0, followed by the data buffer. setting the initial
// sum to buf.size() + 8 (since buf does NOT contain the header) has the same effect.
uint32_t sum = buf.size() + 8;
while (remaining >= 4) {
sum += be_to_h(extract<uint32_t>(buf.substr(buf.size() - remaining, 4)));
remaining -= 4;
}
uint16_t half = 0;
if (remaining >= 2) {
half = be_to_h(extract<uint16_t>(buf.substr(buf.size() - remaining, 2)));
remaining -= 2;
}
uint8_t byte = 0;
if (remaining) {
byte = extract<uint8_t>(buf.substr(buf.size() - remaining, 1));
}
sum += ((byte | (half << 8)) << 8);
return ~sum;
}
nv_u32 m_size;
nv_u32 m_checksum;
string m_key;
unsigned m_write_count = 0;
uint32_t m_raw_size = 0;
bool m_checksum_valid = false;
bool m_magic_valid = false;
bool m_old_style = true;
bool m_size_valid = false;
};
class gwsettings : public encryptable_settings
{
public:
gwsettings(const string& checksum, const csp<bcm2dump::profile>& p,
const string& key, const string& pw)
: encryptable_settings("gwsettings", nv_group::fmt_gws, p),
m_checksum(checksum), m_key(key), m_pw(pw) {}
virtual size_t bytes() const override
{ return m_size.num(); }
virtual size_t data_bytes() const override
{ return bytes() - (m_magic.size() + 6); }
virtual string type() const override
{ return "gwsettings"; }
virtual bool is_valid() const override
{ return m_magic_valid; }
virtual void key(const string& key) override
{ m_key = key; }
virtual string key() const override
{ return m_key; }
virtual void padded(bool padded) override
{ m_padded = padded; }
virtual bool padded() const override
{ return m_padded; }
virtual istream& read(istream& is) override
{
string buf = read_stream(is);
m_checksum_valid = false;
clip_circumfix(buf);
validate_checksum_and_detect_profile(buf);
validate_magic(buf);
m_encrypted = !m_magic_valid;
if (!m_magic_valid && !decrypt_and_detect_profile(buf)) {
m_key = m_pw = "";
return is;
} else if (!m_encrypted) {
m_key = m_pw = "";
}
istringstream istr(buf.substr(m_magic.size()));
read_header(istr, buf.size());
if (!m_checksum_valid) {
validate_checksum(buf.substr(0, m_size.num()), profile());
}
settings::read(istr);
return is;
}
virtual ostream& write(ostream& os) const override
{
if (!profile()) {
throw runtime_error("cannot write file without a profile");
}
ostringstream ostr;
settings::write(ostr);
string buf = ostr.str();
ostr.str("");
ostr.write(m_magic.data(), m_magic.size());
m_version.write(ostr);
#if 1
// 2 bytes for version, 4 for size
nv_u32::write(ostr, m_magic.size() + 6 + buf.size());
#else
m_size.write(ostr);
#endif
buf = ostr.str() + buf;
if (!m_key.empty()) {
buf = gws_encrypt(buf, m_key, m_profile, m_padded);
} else {
buf = gws_checksum(buf, m_profile) + buf;
}
buf = m_circumfix + buf + m_circumfix;
if (!(os.write(buf.data(), buf.size()))) {
throw runtime_error("error while writing data");
}
return os;
}
virtual string header_to_string() const override
{
// special case, because this encryption method has an empty m_key variable
bool encrypted = m_encrypted && (!profile() || profile()->cfg_encryption() != BCM2_CFG_ENC_SUB_16x16);
return group_header_to_string(m_format, to_hex(m_checksum), m_checksum_valid,
m_size.num(), m_size_valid, m_key, encrypted, profile() ? profile()->name() : "",
m_is_auto_profile, m_circumfix);
}
private:
string m_checksum;
void clip_circumfix(string& buf)
{
string top = m_checksum.substr(0, 12);
string btm = buf.substr(buf.size() - 12, 12);
if (top == btm) {
m_circumfix = top;
m_checksum = m_checksum.substr(12) + buf.substr(0, 12);
buf = buf.substr(12, buf.size() - 24);
}
}
void validate_checksum_and_detect_profile(const string& buf)
{
if (this->profile()) {
validate_checksum(buf, this->profile());
} else {
for (auto p : profile::list()) {
if (validate_checksum(buf, p)) {
m_is_auto_profile = true;
m_profile = p;
break;
}
}
}
}
bool validate_checksum(const string& buf, const csp<bcm2dump::profile>& p)
{
m_checksum_valid = (m_checksum == gws_checksum(buf, p));
return m_checksum_valid;
}
bool validate_magic(const string& buf)
{
// The magic values on Sagem 3686 modems (and possibly others) are ISP-dependent:
//
// FAST3686<isp>056t9p48jp4ee6u9ee659jy9e-54e4j6r0j069k-056
//
// Currently known values for <isp>
// DNA
// CLARO
// SFR-PC20
const string magic2_part2 = "056t9p48jp4ee6u9ee659jy9e-54e4j6r0j069k-056";
const vector<string> magics {
"6u9E9eWF0bt9Y8Rw690Le4669JYe4d-056T9p4ijm4EA6u9ee659jn9E-54e4j6rPj069K-670",
"6u9e9ewf0jt9y85w690je4669jye4d-" + magic2_part2,
"6u9e9ewf0jt9y85w690je4669jye4d-056t9p48jp4ee6u9ee659jy9e-54e4j6r0j069k-057",
};
for (const string& magic : magics) {
if (starts_with(buf, magic)) {
m_magic_valid = true;
m_magic = magic;
break;
}
}
if (!m_magic_valid) {
auto pos = buf.find(magic2_part2);
if (pos != string::npos) {
m_magic_valid = true;
m_magic = buf.substr(0, pos + magic2_part2.size());
} else {
auto it = find_if(buf.begin(), buf.end(), [] (char c) {
return c != '-' && !isalnum(c);
});
if (it != buf.end()) {
const auto longest = max_element(magics.begin(), magics.end(), [] (auto a, auto b) {
return a.size() < b.size();
});
string magic(buf.begin(), it);
if (magic.size() >= magic2_part2.size() && magic.size() <= longest->size()) {
logger::v() << "magic detected by brute force" << endl;
m_magic_valid = true;
m_magic = magic;
}
}
}
}
return m_magic_valid;
}
void read_header(istringstream& istr, size_t bufsize)
{
m_size.num(0);
if (!m_version.read(istr) || !m_size.read(istr)) {
throw runtime_error("error while reading header");
}
logger::t("magic=%s\n", m_magic.c_str());
auto v = m_version.num();
logger::t("version=%d.%d, size=%d\n", v >> 8, v & 0xff, m_size.num());
m_size_valid = m_size.num() == bufsize;
if (!m_size_valid && bufsize > m_size.num()) {
if (m_profile->cfg_encryption() == BCM2_CFG_ENC_SUB_16x16 && (m_size.num() + 1) == bufsize && !(bufsize & 1)) {
// this "encryption" mode adds 1 NUL byte if the data size isn't a multiple of 2
m_size_valid = true;
} else {
logger::v("data size exceeds reported file size: %zu vs %d\n", bufsize, m_size.num());
m_size.num(bufsize);
}
}
}
bool decrypt_with_profile(string& buf, const csp<bcm2dump::profile>& p)
{
if (!p || !p->cfg_encryption()) {
return false;
}
vector<string> keys;
if (!m_key.empty()) {
keys.push_back(m_key);
} else if (!m_pw.empty()) {
keys.push_back(p->derive_key(m_pw));
} else {
keys = p->default_keys();
// in case the encryption mode does not require a key
keys.push_back("");
}
for (auto key : keys) {
string tmpsum = m_checksum;
string tmpbuf;
bool padded;
try {
tmpbuf = gws_decrypt(buf, tmpsum, key, p, padded);
} catch (const invalid_argument& e) {
logger::t() << e.what() << endl;
continue;
}
if (validate_magic(tmpbuf)) {
m_key = key;
buf = tmpbuf;
m_padded = padded;
if (!m_checksum_valid) {
m_checksum = tmpsum;
validate_checksum(buf, p);
}
return true;
}
}
return false;
}
bool decrypt_and_detect_profile(string& buf)
{
if (profile()) {
bool ok = decrypt_with_profile(buf, profile());
if (!m_is_auto_profile || ok) {
return ok;
}
}
for (auto p : profile::list()) {
if (decrypt_with_profile(buf, p)) {
m_is_auto_profile = true;
m_profile = p;
return true;
}
}
return false;
}
bool m_is_auto_profile = false;
bool m_checksum_valid = false;
bool m_magic_valid = false;
bool m_size_valid = false;
bool m_encrypted = false;
nv_version m_version;
nv_u32 m_size;
string m_magic;
string m_key;
string m_pw;
string m_circumfix;
bool m_padded = false;
};
/**
* This whole class is a bit of a hack, since the nv_* code wasn't
* really meant for anything else than the permnv/dynnv settings groups.
*/
class boltenv : public encryptable_settings
{
public:
static constexpr uint8_t header_size = 0x1b;
// big endian
static constexpr uint32_t tlv_cheat = 0x011a0000;
// little endian
static constexpr uint32_t magic = 0xbabefeed;
/**
* We're presenting as an nv_compound, but the members don't exactly
* reflect the internal structure. By exposing the tag, and raw value,
* one can "easily" do stuff like renaming or removing variables.
*/
class var : public nv_compound
{
public:
static constexpr int end = 0x00;
static constexpr int var1 = 0x01;
static constexpr int var2 = 0x02;
var() : nv_compound(false, "boltenv-var")
{
m_parts.push_back({ "tag", m_tag });
m_parts.push_back({ "raw", m_raw });
m_parts.push_back({ "flags", m_flags });
}
const uint8_t tag() const { return m_tag->num(); }
virtual const string& name() const override { return m_name; }
virtual size_t bytes() const override
{
// 1 to account for the tag itself, and 1 for flags
return calc_raw_length(m_value) + 2;
}
virtual bool parse(const string& str) override
{
if (!tag()) {
throw runtime_error("can't set this variable");
}
if (calc_raw_length(str) < max_raw_length(tag())) {
m_value = str;
m_raw->str(m_name + "=" + str);
m_set = true;
} else {
throw runtime_error("raw variable size cannot exceed " + ::to_string(max_raw_length(tag())));
}
return true;
}
virtual std::string type() const override
{ return "boltenv-var"; }
virtual istream& read(istream& is) override
{
size_t length = read_header(is);
if (length) {
read_data(is, length);
}