forked from LINBIT/generate-cat-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-cat-file.c
1363 lines (1130 loc) · 35.4 KB
/
generate-cat-file.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
#include <sys/errno.h>
#endif
#if defined(_WINDOWS) || defined(_WIN32) || defined(WIN32)
#include <fcntl.h>
#include <io.h>
#define IS_WINDOWS
#endif
/* DER encoding */
#define INTEGER_TAG 0x02
#define OCTET_STRING_TAG 0x04
#define NULL_TAG 0x05
#define OID_TAG 0x06
#define UTC_TIME_TAG 0x17
#define BMP_STRING_TAG 0x1E
#define SEQUENCE_TAG 0x30 //sub-elements must appear in the definition order
#define SET_TAG 0x31 //sub-elements may appear in any order, regardless of the definition
#define ARRAY_TAG 0xA0
#define SHA1_BYTE_LEN 20
#define SHA1_STR_LEN SHA1_BYTE_LEN * 2
#define UTF16_MAX_LEN 1000
const char *const ERR_OOM = "Out of memory";
//linked-list-node
struct list_node {
struct list_node *next;
void *data;
};
struct oid_data {
/* the OID in human readable form */
char *string;
/* the OID in the end-form(includes header) */
char *bytes;
/* length of the OID in the end-form */
size_t length;
};
struct null {
};
struct algo {
struct oid_data *algo_oid;
struct null a_null;
};
struct an_attribute_data {
char *name;
char *value;
};
struct an_attribute {
struct an_attribute_data data;
bool encode_as_set; /* SET or OCTET_STRING */
};
struct a_file {
char *guid; /* {C689AAB8-8E78-11D0-8C47-00C04FC295EE} */
char *sha1_str; //sha1 string
struct an_attribute name_attribute;
struct an_attribute os_attribute;
bool is_pe;
char sha1_bytes[SHA1_BYTE_LEN]; //sha1 bytes in big endian order
};
struct catalog_list_element {
struct oid_data *catalog_list_oid;
char *a_guid;
char *a_time;
struct oid_data *catalog_list_member_oid;
struct list_node *files;
struct list_node *hwids;
struct an_attribute os_info;
};
struct cert_trust_list {
struct oid_data *cert_trust_oid;
struct catalog_list_element *catalog_list_element;
};
struct pkcs7_data {
struct cert_trust_list cert_trust_list;
/* empty set: using SHA-1 which is default */
struct algo algo;
int an_int;
};
struct pkcs7_toplevel {
struct oid_data *signed_data_oid;
struct pkcs7_data data;
};
struct known_oids {
//cold, used once
struct oid_data signed_data_oid;
//cold, used once (if used)
struct oid_data algo_oid;
//cold, used once
struct oid_data cert_trust_oid;
//cold, used once
struct oid_data catalog_list_oid;
//cold, used once (now, depends on tree)
struct oid_data catalog_list_member_oid;
//hot, x2 per file, per HWID and one more
struct oid_data attribute_name_value_oid;
//warm, per file
struct oid_data member_info_oid;
//warm, per file
struct oid_data spc_oid;
//warm, per file
struct oid_data spc_image_data_oid;
//warm, per file
struct oid_data spc_link_oid;
//warm, per file
struct oid_data spc_algo_oid;
};
static struct known_oids oids;
struct node_data {
size_t length;
};
struct cache {
//each calculation/write advance this to last visited leaf in current branch
//so, on write just after calc for the same branch, store current node before calc and restore it before write
struct list_node *node;
};
size_t buflen = 0;
size_t bufsz = 0;
char *buffer = NULL;
struct cache datacache = { 0 };
void __attribute((noreturn)) fatal(const char *msg)
{
fputs(msg, stderr);
exit(1);
}
size_t append_to_buffer(size_t n, char *data)
{
if (buffer == NULL)
fatal("buffer not initialized\n");
if ((buflen + n) > bufsz)
fatal("insufficient buffer size\n");
memcpy(buffer + buflen, data, n);
buflen += n;
return n;
}
//primitive writers
size_t encode_null(bool write)
{
if (!write)
return 2;
char null_buf[2] = { NULL_TAG, 0x00 };
return append_to_buffer(2, null_buf);
}
size_t encode_empty_set(bool write)
{
if (!write)
return 2;
char empty_set_buf[2] = { SET_TAG, 0x00 };
return append_to_buffer(2, empty_set_buf);
}
size_t encode_integer(int i, bool write)
{
//-128 and 128 (or -1 and 255) both can be encoded in one byte
//read 8.3.2 and 8.3.3 of X.690, take account of NOTE for 8.3.2
//not full two's complement conversion, on storing the original value is used
int tmp = i < 0? ~i : i;
if (write)
{
char int_buf[6] = { INTEGER_TAG, };
if (tmp < 0x80) {
int_buf[1] = 1; /* length (without header) */
int_buf[2] = i & 0xff;
return append_to_buffer(3, int_buf);
}
if (tmp < 0x8000) {
int_buf[1] = 2; /* length */
int_buf[2] = (i >> 8) & 0xff;
int_buf[3] = i & 0xff;
return append_to_buffer(4, int_buf);
}
if (tmp < 0x800000) {
int_buf[1] = 3; /* length */
int_buf[2] = (i >> 16) & 0xff;
int_buf[3] = (i >> 8) & 0xff;
int_buf[4] = i & 0xff;
return append_to_buffer(5, int_buf);
}
if (tmp < 0x80000000) {
int_buf[1] = 4; /* length */
int_buf[2] = (i >> 24) & 0xff;
int_buf[3] = (i >> 16) & 0xff;
int_buf[4] = (i >> 8) & 0xff;
int_buf[5] = i & 0xff;
return append_to_buffer(6, int_buf);
}
}
else
{
if (tmp < 0x80)
return 3;
if (tmp < 0x8000)
return 4;
if (tmp < 0x800000)
return 5;
if (tmp < 0x80000000)
return 6;
}
fatal("can't encode this integer\n");
}
size_t encode_length_to_cache(size_t len, char *buf)
{
if (len < 0x80) {
buf[0] = len;
return 1;
}
if (len < 0x100) {
buf[0] = 0x81; /* 1 more length bytes */
buf[1] = len;
return 2;
}
if (len < 0x10000) {
buf[0] = 0x82; /* 2 more length bytes */
buf[1] = (len >> 8) & 0xff;
buf[2] = len & 0xff;
return 3;
}
if (len < 0x1000000) {
buf[0] = 0x83; /* 3 more length bytes */
buf[1] = (len >> 16) & 0xff;
buf[2] = (len >> 8) & 0xff;
buf[3] = len & 0xff;
return 4;
}
fatal("unsupported tag length\n");
}
size_t encode_tag_and_length(char tag, size_t len, bool write)
{
if (write)
{
char buf[5] = { tag, };
size_t length = 1 + encode_length_to_cache(len, buf + 1);
return append_to_buffer(length, buf);
}
else
{
if (len < 0x80)
return 2;
if (len < 0x100)
return 3;
if (len < 0x10000)
return 4;
if (len < 0x1000000)
return 5;
}
fatal("unsupported tag length\n");
}
size_t sizeof_oid_arc(int arc)
{
if (arc < 0)
fatal("OID arc must not be negative\n");
if (arc < 0x80)
return 1;
if (arc < 0x4000)
return 2;
if (arc < 0x200000)
return 3;
fatal("can't encode this OID arc\n");
}
size_t encode_oid_arc_to_cache(int arc, char *buf)
{
if (arc < 0)
fatal("OID arc must not be negative\n");
if (arc < 0x80) {
buf[0] = arc;
return 1;
}
if (arc < 0x4000) {
buf[0] = ((arc >> 7) & 0x7f) | 0x80 ;
buf[1] = arc & 0x7f;
return 2;
}
if (arc < 0x200000) {
buf[0] = ((arc >> 14) & 0x7f) | 0x80 ;
buf[1] = ((arc >> 7) & 0x7f) | 0x80 ;
buf[2] = arc & 0x7f;
return 3;
}
fatal("can't encode this OID arc\n");
}
size_t encode_oid_arc(int arc)
{
char buf[3];
size_t length = encode_oid_arc_to_cache(arc, buf);
return append_to_buffer(length, buf);
}
// oids encoders
size_t encode_oid(char *oid, bool write)
{
char *next;
size_t length;
int root, sroot, child;
size_t (*oid_arc_handler)(int) = sizeof_oid_arc;
root = strtoul(oid, &next, 10);
if (root == 0 && errno != 0) {
fatal("could not parse OID root arc\n");
}
if (*next != '.') {
fatal("syntax error in OID\n");
}
sroot = strtoul(next+1, &next, 10);
if (sroot == 0 && errno != 0) {
fatal("could not parse OID sub-root arc\n");
}
if (*next != '.' && *next != '\0') {
fatal("syntax error in OID\n");
}
if (write)
oid_arc_handler = encode_oid_arc;
length = oid_arc_handler(root * 40 + sroot);
while (*next != '\0') {
child = strtoul(next+1, &next, 10);
if (child == 0 && errno != 0) {
fatal("could not parse OID child arc\n");
}
if (*next != '.' && *next != '\0') {
fatal("syntax error in OID\n");
}
length += oid_arc_handler(child);
}
return length;
}
size_t encode_oid_to_cache(char *oid, char *buf, size_t buf_sz)
{
size_t length;
char *next;
int root, sroot, child;
root = strtoul(oid, &next, 10);
if (root == 0 && errno != 0) {
fatal("could not parse OID root arc\n");
}
if (*next != '.') {
fatal("syntax error in OID\n");
}
sroot = strtoul(next+1, &next, 10);
if (sroot == 0 && errno != 0) {
fatal("could not parse OID sub-root arc\n");
}
if (*next != '.' && *next != '\0') {
fatal("syntax error in OID\n");
}
length = encode_oid_arc_to_cache(root * 40 + sroot, buf);
while (*next != '\0') {
if ((length + 3) > buf_sz)
fatal("can't encode this OID\n");
child = strtoul(next+1, &next, 10);
if (child == 0 && errno != 0)
fatal("could not parse OID child arc\n");
if (*next != '.' && *next != '\0')
fatal("syntax error in OID\n");
length += encode_oid_arc_to_cache(child, buf + length);
}
return length;
}
//for any oids; byte form is calculated on each write; may be expensive with "hot" oids
size_t encode_plain_oid_with_header(char *oid, bool write)
{
size_t oid_length = encode_oid(oid, false);
size_t head_length = encode_tag_and_length(OID_TAG, oid_length, write);
if (write)
return encode_oid(oid, true) + head_length;
return oid_length + head_length;
}
//for known oids; necessary data is calculated on first request and cached inside the oid object for further usage
size_t encode_known_oid_with_header(struct oid_data *oid, bool write)
{
if (oid->string == NULL || *oid->string == '\0')
fatal("the string value of the known OID must be not NULL nor empty string\n");
if (oid->bytes == NULL)
{
// size of this buffer(128 + 4) is based on
// max length of oid arc in bytes(3 for local encoder) times max known count of arcs(34)
// rounded to nearest power of two
// plus max length of length value(4 for local encoder)
char oid_buf[0x84];
size_t data_length = encode_oid_to_cache(oid->string, oid_buf + 4, 0x80);
size_t head_length = encode_length_to_cache(data_length, oid_buf);
oid->length = data_length + head_length + 1;
oid->bytes = malloc(oid->length);
if (oid->bytes == NULL)
{
fatal(ERR_OOM);
}
oid->bytes[0] = OID_TAG;
memcpy(oid->bytes + 1, oid_buf, head_length);
memcpy(oid->bytes + 1 + head_length, oid_buf + 4, data_length);
}
if (write)
return append_to_buffer(oid->length, oid->bytes);
return oid->length;
}
//generic string writer
size_t encode_tagged_string(char tag, size_t len, char *str, bool write)
{
size_t head_length = encode_tag_and_length(tag, len, write);
if (write)
return append_to_buffer(len, str) + head_length;
return len + head_length;
}
//string converters
size_t encode_string_as_utf16(const char *s, bool write)
{
int i;
if (write)
{
unsigned short utf16[UTF16_MAX_LEN];
for (i = 0; i < UTF16_MAX_LEN && s[i] != '\0'; ++i) {
utf16[i]=(unsigned char)(s[i]);
}
if (i < UTF16_MAX_LEN)
{
utf16[i++] = 0;
return encode_tagged_string(OCTET_STRING_TAG, i * sizeof(utf16[0]), (char *) utf16, true);
}
}
else
{
i = strlen(s);
if (i < UTF16_MAX_LEN)
{
return encode_tagged_string(OCTET_STRING_TAG, (i + 1) * sizeof(unsigned short), NULL, false);
}
}
fatal("string too long\n");
}
size_t encode_string_as_utf16_bmp(const char *s, bool write)
{
int i;
if (write)
{
unsigned short utf16[UTF16_MAX_LEN];
for (i = 0; i < UTF16_MAX_LEN && s[i] != '\0'; ++i) {
utf16[i]=(unsigned char)(s[i]) << 8;
}
if (s[i] == '\0')
{
return encode_tagged_string(BMP_STRING_TAG, i * sizeof(utf16[0]), (char *) utf16, true);
}
}
else
{
i = strlen(s);
if (i <= UTF16_MAX_LEN)
{
return encode_tagged_string(BMP_STRING_TAG, i * sizeof(unsigned short), NULL, false);
}
}
fatal("string too long\n");
}
//generic data encoder
size_t encode_tagged_data(char tag, void *s, size_t a_fn(void *, bool), bool write)
{
struct list_node *this_node = datacache.node->next;
struct node_data *node_data;
if (this_node == NULL)
{
this_node = malloc(sizeof(struct list_node) + sizeof(struct node_data));
if (this_node == NULL)
{
fatal(ERR_OOM);
}
datacache.node->next = this_node;
datacache.node = this_node;
this_node->next = NULL;
this_node->data = this_node + 1;
node_data = this_node->data;
node_data->length = a_fn(s, false);
}
node_data = this_node->data;
size_t head_length = encode_tag_and_length(tag, node_data->length, write);
if (write)
{
//(re-)set datacache.node to proper reference
datacache.node = this_node;
return a_fn(s, true) + head_length;
}
return node_data->length + head_length;
}
//hi-level encoders
size_t encode_algo(void *p, bool write)
{
size_t length = 0;
length += encode_known_oid_with_header(&oids.algo_oid, write);
length += encode_null(write);
return length;
}
size_t encode_algo_sequence(void *p, bool write)
{
return encode_tagged_data(SEQUENCE_TAG, p, encode_algo, write);
}
size_t encode_attribute_name_and_value(void *p, bool write)
{
struct an_attribute_data *attr = p;
size_t length = 0;
length += encode_string_as_utf16_bmp(attr->name, write);
//mscat.h
// CRYPTCAT_ATTR_AUTHENTICATED
// | CRYPTCAT_ATTR_DATAASCII
// | | CRYPTCAT_ATTR_NAMEASCII
// v v v
length += encode_integer(0x10010001, write);
length += encode_string_as_utf16(attr->value, write);
return length;
}
size_t encode_attribute_sequence(void *p, bool write)
{
return encode_tagged_data(SEQUENCE_TAG, p, encode_attribute_name_and_value, write);
}
size_t encode_attribute(void *p, bool write)
{
struct an_attribute *attr = p;
size_t length = 0;
length += encode_known_oid_with_header(&oids.attribute_name_value_oid, write);
length += encode_tagged_data(attr->encode_as_set? SET_TAG : OCTET_STRING_TAG, p, encode_attribute_sequence, write);
return length;
}
size_t encode_member_info(void *p, bool write)
{
struct a_file *file = p;
size_t length = 0;
length += encode_string_as_utf16_bmp(file->guid, write);
//CryptCATOpen() ms docs : version, can be 0x100, 0x200
length += encode_integer(0x200, write);
return length;
}
size_t encode_member_info_sequence(void *p, bool write)
{
return encode_tagged_data(SEQUENCE_TAG, p, encode_member_info, write);
}
size_t encode_member_info_oid(void *p, bool write)
{
size_t length = 0;
length += encode_known_oid_with_header(&oids.member_info_oid, write);
length += encode_tagged_data(SET_TAG, p, encode_member_info_sequence, write);
return length;
}
size_t encode_spc_image_data(void *p, bool write)
{
size_t length = encode_known_oid_with_header(&oids.spc_image_data_oid, write);
//*
if (!write)
return length + 0x28;
// <<<obsolete>>>: vanila vv vv vv vv | value utf-16-bmp, to the end
char image_data[0x28] = { 0x30, 0x26, 0x03, 0x02, 0x05, 0xA0, 0xA0, 0x20, 0xA2, 0x1E, 0x80, 0x1C, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x4F, 0x00, 0x62, 0x00, 0x73, 0x00, 0x6F, 0x00, 0x6C, 0x00, 0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3E, 0x00, 0x3E, 0x00, 0x3E };
return length + append_to_buffer(sizeof(image_data), image_data);
}
size_t encode_spc_link(void *p, bool write)
{
size_t length = encode_known_oid_with_header(&oids.spc_link_oid, write);
if (!write)
return length + 0x20;
// <<<obsolete>>>: vanila vv vv | value utf-16-bmp, to the end
char link_data[0x20] = { 0xA2, 0x1E, 0x80, 0x1C, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x4F, 0x00, 0x62, 0x00, 0x73, 0x00, 0x6F, 0x00, 0x6C, 0x00, 0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3E, 0x00, 0x3E, 0x00, 0x3E };
return length + append_to_buffer(sizeof(link_data), link_data);
}
int hexdigit(char c)
{
if (c>='0' && c<='9')
return c-'0';
c = toupper(c);
if (c>='A' && c<='F')
return c-'A'+10;
fatal("invalid hex digit\n");
}
size_t encode_spc_algo_oid(void *p, bool write)
{
size_t length = 0;
length += encode_known_oid_with_header(&oids.spc_algo_oid, write);
length += encode_null(write);
return length;
}
size_t encode_spc_algo(void *p, bool write)
{
struct a_file *file = p;
size_t length = 0;
length += encode_tagged_data(SEQUENCE_TAG, p, encode_spc_algo_oid, write);
length += encode_tagged_string(OCTET_STRING_TAG, SHA1_BYTE_LEN, file->sha1_bytes, write);
return length;
}
size_t encode_spc(void *p, bool write)
{
struct a_file *file = p;
size_t length = 0;
length += encode_tagged_data(SEQUENCE_TAG, p, file->is_pe? encode_spc_image_data : encode_spc_link, write);
length += encode_tagged_data(SEQUENCE_TAG, p, encode_spc_algo, write);
return length;
}
size_t encode_spc_sequence(void *p, bool write)
{
return encode_tagged_data(SEQUENCE_TAG, p, encode_spc, write);
}
size_t encode_spc_oid(void *p, bool write)
{
size_t length = 0;
length += encode_known_oid_with_header(&oids.spc_oid, write);
length += encode_tagged_data(SET_TAG, p, encode_spc_sequence, write);
return length;
}
size_t encode_file_attributes(void *p, bool write)
{
struct a_file *file = p;
size_t length = 0;
//Inf2Cat like order
length += encode_tagged_data(SEQUENCE_TAG, &file->os_attribute, encode_attribute, write);
length += encode_tagged_data(SEQUENCE_TAG, &file->name_attribute, encode_attribute, write);
if (file->is_pe)
{
length += encode_tagged_data(SEQUENCE_TAG, p, encode_member_info_oid, write);
length += encode_tagged_data(SEQUENCE_TAG, p, encode_spc_oid, write);
}
else
{
length += encode_tagged_data(SEQUENCE_TAG, p, encode_spc_oid, write);
length += encode_tagged_data(SEQUENCE_TAG, p, encode_member_info_oid, write);
}
/**/
return length;
}
size_t encode_one_file(void *p, bool write)
{
struct a_file *file = p;
size_t length = 0;
length += encode_string_as_utf16(file->sha1_str, write);
length += encode_tagged_data(SET_TAG, p, encode_file_attributes, write);
return length;
}
size_t encode_files(void *p, bool write)
{
struct list_node *node = p;
size_t length = 0;
while (node) {
length += encode_tagged_data(SEQUENCE_TAG, node->data, encode_one_file, write);
node = node->next;
}
return length;
}
size_t encode_catalog_list_member_oid(void *p, bool write)
{
size_t length = 0;
length += encode_known_oid_with_header(&oids.catalog_list_member_oid, write);
length += encode_null(write);
return length;
}
size_t encode_catalog_list_oid(void *p, bool write)
{
return encode_known_oid_with_header(&oids.catalog_list_oid, write);
}
//specialized version of encode_attribute()
size_t encode_one_hwid(void *p, bool write)
{
size_t length = 0;
length += encode_known_oid_with_header(&oids.attribute_name_value_oid, write);
length += encode_tagged_data(OCTET_STRING_TAG, p, encode_attribute_sequence, write);
return length;
}
size_t encode_hwids(struct list_node *hwid, bool write)
{
size_t length = 0;
while (hwid) {
length += encode_tagged_data(SEQUENCE_TAG, hwid->data, encode_one_hwid, write);
hwid = hwid->next;
}
return length;
}
size_t encode_global_attributes2(void *p, bool write)
{
struct catalog_list_element *elem = p;
size_t length = 0;
length += encode_tagged_data(SEQUENCE_TAG, &elem->os_info, encode_attribute, write);
length += encode_hwids(elem->hwids, write);
return length;
}
size_t encode_global_attributes(void *p, bool write)
{
return encode_tagged_data(SEQUENCE_TAG, p, encode_global_attributes2, write);
}
size_t encode_catalog_list_elements(void *p, bool write)
{
struct catalog_list_element *elem = p;
size_t length = 0;
length += encode_tagged_data(SEQUENCE_TAG, p, encode_catalog_list_oid, write);
length += encode_tagged_string(OCTET_STRING_TAG, 16, elem->a_guid, write);
length += encode_tagged_string(UTC_TIME_TAG, 13, elem->a_time, write);
length += encode_tagged_data(SEQUENCE_TAG, p, encode_catalog_list_member_oid, write);
length += encode_tagged_data(SEQUENCE_TAG, elem->files, encode_files, write);
length += encode_tagged_data(ARRAY_TAG, p, encode_global_attributes, write);
return length;
}
size_t encode_catalog_list_sequence(void *p, bool write)
{
return encode_tagged_data(SEQUENCE_TAG, p, encode_catalog_list_elements, write);
}
size_t encode_cert_trust_list(void *p, bool write)
{
struct cert_trust_list *cert = p;
size_t length = 0;
length += encode_known_oid_with_header(cert->cert_trust_oid, write);
length += encode_tagged_data(ARRAY_TAG, cert->catalog_list_element, encode_catalog_list_sequence, write);
return length;
}
size_t encode_pkcs7_data(void *p, bool write)
{
struct pkcs7_data *data = p;
size_t length = 0;
length += encode_integer(data->an_int, write); //version?
length += encode_empty_set(write);
length += encode_tagged_data(SEQUENCE_TAG, &data->cert_trust_list, encode_cert_trust_list, write);
length += encode_empty_set(write);
return length;
}
size_t encode_pkcs7_sequence(void *p, bool write)
{
return encode_tagged_data(SEQUENCE_TAG, p, encode_pkcs7_data, write);
}
size_t encode_pkcs7_toplevel(void *p, bool write)
{
struct pkcs7_toplevel *sdat = p;
size_t length = 0;
length += encode_known_oid_with_header(sdat->signed_data_oid, write);
length += encode_tagged_data(ARRAY_TAG, &sdat->data, encode_pkcs7_sequence, write);
return length;
}
void free_allocated(struct pkcs7_toplevel *sdat)
{
struct list_node *next_node;
struct list_node *this_node;
struct node_data *node_data;
next_node = datacache.node;
datacache.node = NULL;
while (next_node)
{
this_node = next_node;
next_node = this_node->next;
node_data = this_node->data;
node_data->length = 0;
this_node->data = NULL;
this_node->next = NULL;
free(this_node);
}
struct a_file *file_data;
next_node = sdat->data.cert_trust_list.catalog_list_element->files;
sdat->data.cert_trust_list.catalog_list_element->files = NULL;
while (next_node)
{
this_node = next_node;
next_node = this_node->next;
file_data = this_node->data;
file_data->name_attribute.data.value = NULL;
this_node->data = NULL;
this_node->next = NULL;
free(this_node);
}
struct an_attribute_data *hwid_data;
next_node = sdat->data.cert_trust_list.catalog_list_element->hwids;
sdat->data.cert_trust_list.catalog_list_element->hwids = NULL;
while (next_node)
{
this_node = next_node;
next_node = this_node->next;
hwid_data = this_node->data;
free(hwid_data->name);
hwid_data->name = NULL;
hwid_data->value = NULL;
this_node->data = NULL;
this_node->next = NULL;
free(this_node);
}
free(sdat->data.cert_trust_list.catalog_list_element->a_time);
sdat->data.cert_trust_list.catalog_list_element->a_time = NULL;
free(sdat->data.cert_trust_list.catalog_list_element->a_guid);
sdat->data.cert_trust_list.catalog_list_element->a_guid = NULL;
free(sdat->data.cert_trust_list.catalog_list_element);
}
void create_binary_tree(struct pkcs7_toplevel *sdat)
{
buflen = 0;
/* store root_node, as it will be used for buffer size and also as reset point */
struct list_node *root_node = datacache.node;
struct node_data *root_data = root_node->data;
/* compute sufficient buffer size and store it in root node */
root_data->length = encode_pkcs7_toplevel(sdat, false);
bufsz =
encode_tag_and_length(SEQUENCE_TAG, root_data->length, false)
+ root_data->length
;
if (buflen != 0)
fatal("unexpected write\n");
/* place for extra limitation
take a note:
while limitation of the redirection usually not reachable or depends on the target file system
it's much lower for pipes https://unix.stackexchange.com/a/11954
*/
/* create buffer of computed size */
buffer = malloc(bufsz);
if (buffer == NULL)
fatal(ERR_OOM);
/* reset cache node to root_node */
datacache.node = root_node;
/* write data to buffer */
encode_tag_and_length(SEQUENCE_TAG, root_data->length, true);
encode_pkcs7_toplevel(sdat, true);
/* check written data length */
if (buflen != bufsz)
fatal("length mismatch\n");
}
void __attribute((noreturn)) usage_and_exit(void)
{
fprintf(stderr, "Usage: generate_cat_file -h <hardware-ids> [-O OS string] [-A OS attribute string] [-G <cat-guid>] [-T <generation-time>] file-with-hash1 [ file-with-hash2 ... ]\n");
fprintf(stderr, "Generates a Microsoft Security Catalog (\".cat\") file.\n");
fprintf(stderr, "hardware-ids is comma separated list\n");
fprintf(stderr, "generation-time has the format YYmmddHHMMSSZ, Z is constant, means 0 timezone\n");
fprintf(stderr, "file-with-hash has the format filename:sha1-hash-in-hex[:PE]\n");
fprintf(stderr, "Use osslsigncode to sign it afterwards.\n");
exit(1);
}