-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.c
1205 lines (1024 loc) · 35.5 KB
/
common.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
// SPDX-License-Identifier: MIT
#include "common.h"
#include <errno.h>
#include <fcntl.h>
#include <linux/nvme_ioctl.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
enum log_level current_log_level = ERROR;
void LOG_HEX(const void *ptr, unsigned len)
{
const char *buf = ptr;
for (int i = 0; i < len; i++) {
if (i && !(i % 16))
fprintf(stderr, "\n");
fprintf(stderr, "%02x", (unsigned char)buf[i]);
}
fprintf(stderr, "\n");
}
int hex_add(unsigned char *a, size_t a_len, size_t b)
{
size_t i = 0;
int extra = 0;
while ((b > 0 || extra > 0) && i < a_len) {
int next_extra = (a[a_len - i - 1] + (b % 256) + extra) > 255;
a[a_len - i - 1] += (b % 256) + extra;
b /= 256;
i += 1;
extra = next_extra;
}
return b > 0;
}
const char *error_to_string(enum MethodStatusCode msc)
{
switch (msc) {
case MSC_SUCCESS:
return "MSC_SUCCESS";
case MSC_NOT_AUTHORIZED:
return "MSC_NOT_AUTHORIZED";
case MSC_SP_BUSY:
return "MSC_SP_BUSY";
case MSC_SP_FAILED:
return "MSC_SP_FAILED";
case MSC_SP_DISABLED:
return "MSC_SP_DISABLED";
case MSC_SP_FROZEN:
return "MSC_SP_FROZEN";
case MSC_NO_SESSIONS_AVAILABLE:
return "MSC_NO_SESSIONS_AVAILABLE";
case MSC_UNIQUENESS_CONFLICT:
return "MSC_UNIQUENESS_CONFLICT";
case MSC_INSUFFICIENT_SPACE:
return "MSC_INSUFFICIENT_SPACE";
case MSC_INSUFFICIENT_ROWS:
return "MSC_INSUFFICIENT_ROWS";
case MSC_INVALID_PARAMETER:
return "MSC_INVALID_PARAMETER";
case MSC_TPER_MALFUNCTION:
return "MSC_TPER_MALFUNCTION";
case MSC_TRANSACTION_FAILURE:
return "MSC_TRANSACTION_FAILURE";
case MSC_RESPONSE_OVERFLOW:
return "MSC_RESPONSE_OVERFLOW";
case MSC_AUTHORITY_LOCKED_OUT:
return "MSC_AUTHORITY_LOCKED_OUT";
case MSC_FAIL:
return "MSC_FAIL";
default:
return "UNKNOWN";
}
}
static int tcg_discovery_0_process_feature(struct disk_device *dev, void *data, int feature_code)
{
/*
* Core spec feature codes
* 0x0000 reserved
* 0x0001 TPer
* 0x0002 locking
* 0x0003 – 0x00ff reserved
* 0x0100 – 0x03ff SSCs
* 0x0400 - 0xbfff reserved
* 0xC000 - 0xffff vendor specific
*/
if (feature_code == 0x0001) {
struct level_0_discovery_tper_feature *body = data;
dev->features.tper = *body;
} else if (feature_code == 0x0002) {
struct level_0_discovery_lockin_feature *body = data;
dev->features.locking = *body;
} else if (feature_code == 0x0003) {
struct level_0_discovery_geometry_feature *body = data;
dev->features.geometry = *body;
// } else if (feature_code == 0x0004) { /* Secure messaging (TLS) */
} else if (feature_code == 0x0005) {
struct level_0_discovery_siis_feature *body = data;
dev->features.siis = *body;
// } else if (feature_code == 0x0100) { /* Enterprise SSC */
} else if (feature_code == 0x0200) {
struct level_0_discovery_opal_1_feature *body = data;
//dev->base_com_id = be16_to_cpu(body->base_comID);
dev->features.opal1 = *body;
} else if (feature_code == 0x0201) {
struct level_0_discovery_single_user_mode_feature *body = data;
dev->features.single_user_mode = *body;
} else if (feature_code == 0x0202) {
struct level_0_discovery_data_store_feature *body = data;
dev->features.data_store = *body;
} else if (feature_code == 0x0203) {
struct level_0_discovery_opal_2_feature *body = data;
dev->features.opal2 = *body;
dev->base_com_id = be16_to_cpu(body->base_comID);
// } else if (feature_code == 0x0301) { /* Opalite SSC */
} else if (feature_code == 0x0302) {
struct level_0_discovery_pyrite_feature *body = data;
dev->features.pyrite1 = *body;
dev->base_com_id = be16_to_cpu(body->base_comID);
} else if (feature_code == 0x0303) {
struct level_0_discovery_pyrite_feature *body = data;
dev->features.pyrite2 = *body;
dev->base_com_id = be16_to_cpu(body->base_comID);
// } else if (feature_code == 0x0304) { /* Ruby SSC */
// } else if (feature_code == 0x0305) { /* Key per IO SSC */
// } else if (feature_code == 0x0401) { /* Locking LBA Ranges Control */
} else if (feature_code == 0x0402) {
struct level_0_discovery_block_sid_authentication_feature *body = data;
dev->features.block_sid_authentication = *body;
} else if (feature_code == 0x0403) {
struct level_0_discovery_ns_locking_feature *body = data;
dev->features.ns_locking = *body;
} else if (feature_code == 0x0404) {
struct level_0_discovery_supported_data_removal_mechanism_feature *body = data;
dev->features.supported_data_removal_mechanism = *body;
} else if (feature_code == 0x0405) {
struct level_0_discovery_ns_geometry_feature *body = data;
dev->features.ns_geometry = *body;
// } else if (feature_code == 0x0407) { /* Shadow MBR for multiple namespaces */
// } else if (feature_code == 0x0409) { /* CPIN enhancements */
// } else if (feature_code == 0x040a) { /* Namespace Key Per I/O Capabilities */
} else {
struct level_0_discovery_feature_shared *body = data;
if (dev->features.unknown_len + body->length + 4 < DISK_DEVICE_UNKNOWN_FEATURE_MAX_LENGTH) {
memcpy(dev->features.unknown + dev->features.unknown_len, data, body->length + 4);
} else {
LOG(ERROR, "Too many feature descriptors found, ignoring the rest. (flen:%zu, blen:%u)\n",
dev->features.unknown_len, body->length);
return 1;
}
dev->features.unknown_len += body->length + 4;
}
return 0;
}
static int tcg_discovery_0_process_response(struct disk_device *dev, void *data)
{
int err = 0;
struct level_0_discovery_header *header = data;
uint32_t offset = sizeof(struct level_0_discovery_header);
dev->features.discovery0_revision = be32_to_cpu(header->revision);
while (offset < be32_to_cpu(header->length)) {
struct level_0_discovery_feature_shared *body =
(struct level_0_discovery_feature_shared *)((unsigned char *)data + offset);
if ((err = tcg_discovery_0_process_feature(dev, body, be16_to_cpu(body->feature_code)))) {
return err;
}
offset += body->length + sizeof(struct level_0_discovery_feature_shared);
}
return err;
}
static void log_packet_data(const unsigned char *response, int comID)
{
int l = 0;
const char *name;
size_t length;
bool session;
if (comID < 0x800) {
// FIXME: Discovery 0 or TPer reset
length = 254;
session = false;
} else {
// These should be from initiated session with packet headers
const struct packet_headers *headers = (const struct packet_headers *)response;
length = sizeof(struct packet_headers) + be32_to_cpu(headers->data_subpacket.length);
session = true;
}
for (int x = 0; x < length; ++x) {
if (session && x == sizeof(struct packet_headers)) {
LOG_C(EVERYTHING, "| ");
}
if (l == 2) {
LOG_C(EVERYTHING, "%02x(%s) ", response[x], error_to_string(response[x]));
l = 0;
continue;
} else if (l > 0) {
l++;
}
switch (response[x]) {
case END_OF_DATA_TOKEN:
name = "EOD";
l = 1;
break;
case START_LIST_TOKEN:
name = "SL";
break;
case END_LIST_TOKEN:
name = "EL";
break;
case START_NAME_TOKEN:
name = "SN";
break;
case END_NAME_TOKEN:
name = "EN";
break;
case CALL_TOKEN:
name = "CALL";
break;
case END_OF_SESSION_TOKEN:
name = "EOS";
break;
default:
name = NULL;
break;
}
if (name) {
LOG_C(EVERYTHING, "%02x[%s] ", response[x], name);
} else {
LOG_C(EVERYTHING, "%02x ", response[x]);
}
}
LOG_C(EVERYTHING, "\n");
}
static int nvme_security_command(int fd, uint8_t *buffer, size_t buffer_len,
enum TrustedCommandDirection direction,
int protocol, int comID)
{
int err = 0;
uintptr_t buffer_ptr = (uintptr_t)buffer;
struct nvme_admin_cmd cmd = { 0 };
// The structure of IF-RECV (IF-SEND) is described in 5.25 (5.26) of NVMe Base Specification.
// The values for SP Specific are found in TCG SIIS.
cmd.opcode = direction == IF_RECV ? NVME_SECURITY_RECEIVE : NVME_SECURITY_SEND;
cmd.addr = (uint64_t)buffer_ptr;
cmd.data_len = buffer_len;
// Security Protocol (SECP) | SP Specific | reserved
cmd.cdw10 = (protocol << 24) | (comID << 8);
// Allocation Length (AL)
cmd.cdw11 = buffer_len;
err = ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
LOG(EVERYTHING, "Packet %s:\n", direction == IF_RECV ? "received" : "sent");
log_packet_data(buffer, comID);
if (err != 0) {
LOG(ERROR, "Problem with sending NVMe Security command: %s.\n", strerror(errno));
}
return err;
}
static int scsi_send_cdb(int fd, uint8_t *cdb, size_t cdb_len,
uint8_t *response, size_t response_len,
enum TrustedCommandDirection direction, int comID)
{
int err = 0;
uint8_t sense[32] = { 0 };
struct sg_io_hdr sg = {
.interface_id = 'S',
.dxfer_direction = direction == IF_RECV ? SG_DXFER_FROM_DEV : SG_DXFER_TO_DEV,
.cmdp = cdb,
.cmd_len = cdb_len,
.dxferp = response,
.dxfer_len = response_len,
.timeout = 60000,
.sbp = sense,
.mx_sb_len = sizeof(sense),
};
err = ioctl(fd, SG_IO, &sg);
LOG(EVERYTHING, "Packet %s:\n", direction == IF_RECV ? "received" : "sent");
log_packet_data(response, comID);
if (err != 0) {
LOG(ERROR, "Problem with sending ATA Trusted command: %s.\n", strerror(errno));
}
if (sense[0] != 0 || sense[1] != 0) {
err = -1;
LOG(ERROR, "Received sense code: ");
for (size_t i = 0; i < sizeof(sense); ++i) {
LOG_C(ERROR, "%02x ", sense[i]);
}
LOG_C(ERROR, "\n");
}
return err;
}
static int ata_trusted_command(int fd, uint8_t *response, size_t response_len,
enum TrustedCommandDirection direction,
int protocol, int sp_specific)
{
// ACS-3
struct sg_cdb_ata_pass_through_12 cdb = {
.operation_code = ATA_PASS_THROUGH_12_OPERATION_CODE,
.protocol = direction == IF_RECV ? ATA_PROTOCOL_PIO_DATA_IN : ATA_PROTOCOL_PIO_DATA_OUT,
.t_dir = direction == IF_RECV ? ATA_PASS_THROUGH_RECEIVE : ATA_PASS_THROUGH_SEND,
.byt_blok = 1,
.t_length = 2,
.trusted_receive.security_protocol = protocol & 0xFF,
.trusted_receive.transfer_length = cpu_to_le16(response_len / 512),
.trusted_receive.sp_specific = cpu_to_le16(sp_specific),
.trusted_receive.command = direction == IF_RECV ? ATA_TRUSTED_RECEIVE : ATA_TRUSTED_SEND,
};
return scsi_send_cdb(fd, (uint8_t *)&cdb, sizeof(cdb), response, response_len, direction, sp_specific);
}
static int scsi_security_protocol(int fd, uint8_t *response, size_t response_len,
enum TrustedCommandDirection direction,
int protocol, int protocol_specific)
{
struct scsi_security_protocol cdb = {
.operation_code = direction == IF_RECV ? SCSI_SECURITY_PROTOCOL_IN : SCSI_SECURITY_PROTOCOL_OUT,
.security_protocol = protocol,
.security_protocol_specific = cpu_to_be16(protocol_specific),
.allocation_length = cpu_to_be32(response_len),
};
return scsi_send_cdb(fd, (uint8_t *)&cdb, sizeof(cdb), response, response_len, direction, protocol_specific);
}
int trusted_command(struct disk_device *dev, uint8_t *msg, size_t msg_len, enum TrustedCommandDirection direction,
int protocol, int comID)
{
if (dev->type == NVME)
return nvme_security_command(dev->fd, msg, msg_len, direction, protocol, comID);
else if (dev->type == SATA)
return ata_trusted_command(dev->fd, msg, msg_len, direction, protocol, comID);
else if (dev->type == SCSI)
return scsi_security_protocol(dev->fd, msg, msg_len, direction, protocol, comID);
else {
LOG(ERROR, "Unsupported device interface.\n");
return 1;
}
}
void start_list(unsigned char *buffer, size_t *i)
{
buffer[*i] = START_LIST_TOKEN;
*i += 1;
}
void end_list(unsigned char *buffer, size_t *i)
{
buffer[*i] = END_LIST_TOKEN;
*i += 1;
}
void start_name(unsigned char *buffer, size_t *i)
{
buffer[*i] = START_NAME_TOKEN;
*i += 1;
}
void end_name(unsigned char *buffer, size_t *i)
{
buffer[*i] = END_NAME_TOKEN;
*i += 1;
}
void call_token(unsigned char *buffer, size_t *i)
{
buffer[*i] = CALL_TOKEN;
*i += 1;
}
void end_of_data(unsigned char *buffer, size_t *i)
{
buffer[*i] = END_OF_DATA_TOKEN;
*i += 1;
}
void end_session_token(unsigned char *buffer, size_t *i)
{
buffer[*i] = END_OF_SESSION_TOKEN;
*i += 1;
}
void method_status_list(unsigned char *buffer, size_t *i)
{
buffer[*i] = START_LIST_TOKEN;
*i += 1;
for (int x = 0; x < 3; ++x) {
buffer[*i] = 0x00;
*i += 1;
}
buffer[*i] = END_LIST_TOKEN;
*i += 1;
}
void tiny_atom(unsigned char *buffer, size_t *i, unsigned char S, unsigned char V)
{
buffer[*i] = 0b0 << 7 | (S & 0x1) << 6 | V;
*i += 1;
}
void short_atom(unsigned char *buffer, size_t *i, unsigned char B, unsigned char S, const uint8_t *V, size_t V_len)
{
buffer[*i] = 0b10 << 6 | (B & 0x1) << 5 | (S & 0x1) << 4 | V_len;
*i += 1;
memcpy(buffer + *i, V, V_len);
*i += V_len;
}
void medium_atom(unsigned char *buffer, size_t *i, unsigned char B, unsigned char S, const uint8_t *V, size_t V_len)
{
buffer[*i] = 0b110 << 5 | (B & 0x1) << 4 | (S & 0x1) << 3 | V_len >> 8;
buffer[*i + 1] = 0b11111111 & V_len;
*i += 2;
memcpy(buffer + *i, V, V_len);
*i += V_len;
}
uint64_t parse_int(const unsigned char *buffer, size_t *i)
{
uint64_t result = 0;
if ((buffer[*i] & 0b10000000) == 0b00000000) {
result = buffer[*i] & 0b00111111;
*i += 1;
return result;
} else if ((buffer[*i] & 0b11000000) == 0b10000000) {
size_t len = buffer[*i] & (0b00011111);
*i += 1;
for (size_t j = 0; j < len; ++j) {
result |= buffer[*i] << (((len - 1) - j) * 8);
*i += 1;
}
return result;
} else {
return UINT64_MAX;
}
}
int process_method_response(const unsigned char *buffer, size_t buffer_len)
{
uint8_t status_code = 0;
const struct packet_headers *headers = (const struct packet_headers *)buffer;
const unsigned char *data = NULL;
size_t i = 0, data_length = 0, backtracking_length;
if (buffer_len < sizeof(struct packet_headers)) {
LOG(ERROR, "Buffer was not large enough for mandatory headers.\n");
return -1;
}
if (be32_to_cpu(headers->com_packet.length) == 0) {
LOG(ERROR, "ComPacket with 0 length (outstanding %i, min_transfer %i).\n",
be32_to_cpu(headers->com_packet.outstanding_data),
be32_to_cpu(headers->com_packet.min_transfer));
return -1;
}
if (be32_to_cpu(headers->packet.length) == 0) {
LOG(ERROR, "Packet with 0 length (ComPacket length %i).\n",
be32_to_cpu(headers->com_packet.length));
return -1;
}
if (be32_to_cpu(headers->data_subpacket.length) == 0) {
LOG(ERROR, "SubPacket with 0 length.\n");
return -1;
}
data_length = be32_to_cpu(headers->data_subpacket.length);
data = buffer + sizeof(struct packet_headers);
if (buffer_len < sizeof(struct packet_headers) + data_length) {
LOG(ERROR, "Buffer was not large enough for the response.\n");
return -1;
}
if (data[i] == END_OF_SESSION_TOKEN) {
return 0;
}
if (data[i] == CALL_TOKEN) {
i += 1;
// session manager method
if (data[i++] != 0xa8) {
return 1;
}
if (memcmp(data + i, SMUID, 8) != 0) {
LOG(ERROR, "Got SMUID method from non-SMUID invoker.\n");
return -1;
}
i += 8; // invokid uid
if (data[i++] != 0xa8) {
return 1;
}
if (memcmp(data + i, METHOD_CLOSE_SESSION_UID, 8) == 0) {
LOG(ERROR, "Probably unexpected close session.\n");
return -1;
}
i += 8; // method uid
}
backtracking_length = 8;
if (backtracking_length > data_length - 7) {
backtracking_length = data_length - 7;
}
for (size_t i = 0; i < backtracking_length; ++i) {
const unsigned char *data_tail = data + data_length - 7 - i;
if (data_tail[0] != END_LIST_TOKEN ||
data_tail[1] != END_OF_DATA_TOKEN ||
data_tail[2] != START_LIST_TOKEN ||
data_tail[4] != 00 ||
data_tail[5] != 00 ||
data_tail[6] != END_LIST_TOKEN) {
if (data_tail[6] == EMPTY_ATOM_TOKEN) {
continue;
} else {
LOG(ERROR, "Received unexpected tokens.\n");
return -1;
}
}
if ((status_code = data_tail[3]) != MSC_SUCCESS) {
LOG(ERROR, "Received non-successful status code: %s.\n", error_to_string(status_code));
}
return status_code;
}
return -1;
}
int invoke_method(struct disk_device *dev, unsigned char *buff_in, size_t buff_in_len, unsigned char *buff_out,
size_t buff_out_len)
{
int err = 0;
if ((err = trusted_command(dev, buff_in, buff_in_len, IF_SEND, TCG_PROTOCOL_ID_1, dev->base_com_id))) {
LOG(ERROR, "Failed to send command: %i\n", err);
return err;
}
if ((err = trusted_command(dev, buff_out, buff_out_len, IF_RECV, TCG_PROTOCOL_ID_1, dev->base_com_id))) {
LOG(ERROR, "Failed to receive command: %i\n", err);
return err;
}
if ((err = process_method_response(buff_out, buff_out_len))) {
LOG(ERROR, "Received bad response: %i.\n", err);
return err;
}
return err;
}
void prepare_headers(unsigned char *buffer, size_t *i, struct disk_device *dev)
{
struct packet_headers *headers = (void *)(unsigned char *)buffer;
headers->com_packet.comid = cpu_to_be16(dev->base_com_id);
headers->packet.session = dev->sp_session_id | (uint64_t)dev->host_session_id << 32;
*i += sizeof(struct packet_headers);
}
void prepare_method(unsigned char *buffer, size_t *i, struct disk_device *dev, const unsigned char *invoking_uid,
const unsigned char *method_uid)
{
prepare_headers(buffer, i, dev);
call_token(buffer, i);
short_atom(buffer, i, 1, 0, invoking_uid, 8);
short_atom(buffer, i, 1, 0, method_uid, 8);
start_list(buffer, i);
}
void finish_headers(unsigned char *buffer, size_t *i)
{
struct packet_headers *headers = (void *)(((unsigned char *)buffer));
// Data subpacket does not want aligned length.
headers->data_subpacket.length = cpu_to_be32(*i - sizeof(struct packet_headers));
if ((*i % 4) != 0)
*i += 4 - (*i % 4);
headers->com_packet.length = cpu_to_be32(*i - sizeof(struct com_packet));
headers->packet.length = cpu_to_be32(be32_to_cpu(headers->com_packet.length) - sizeof(struct packet));
if ((*i % PADDING_ALIGNMENT) != 0)
*i += PADDING_ALIGNMENT - (*i % PADDING_ALIGNMENT);
}
void finish_method(unsigned char *buffer, size_t *i)
{
end_list(buffer, i);
end_of_data(buffer, i);
method_status_list(buffer, i);
finish_headers(buffer, i);
}
int do_level_0_discovery(struct disk_device *dev)
{
int err = 0;
uint8_t response[2048] = { 0 };
LOG(INFO, "Sending discovery0 command.\n");
if ((err = trusted_command(dev, response, sizeof(response), IF_RECV, TCG_PROTOCOL_ID_1,
TCG_LEVEL_0_DISCOVERY_COMID))) {
return err;
}
/* Wipe previous response data */
memset(&dev->features, 0, sizeof(dev->features));
if ((err = tcg_discovery_0_process_response(dev, response))) {
return err;
}
return err;
}
static int generate_start_session_method(struct disk_device *dev, unsigned char *buffer, size_t *i, const unsigned char *spid,
size_t spid_len, const unsigned char *host_challenge, size_t host_challenge_len,
const unsigned char *host_signing_authority, size_t host_signing_authority_len)
{
const int host_session_id = 1;
const int write_session = 1;
/*
5.2.3.1 StartSession Method
SMUID.StartSession [
HostSessionID : uinteger,
spid : uidref {SPObjectUID},
Write : boolean,
HostChallenge = bytes,
HostExchangeAuthority = uidref {AuthorityObjectUID},
HostExchangeCert = bytes,
HostSigningAuthority = uidref {AuthorityObjectUID},
HostSigningCert = bytes,
SessionTimeout = uinteger,
TransTimeout = uinteger,
InitialCredit = uinteger,
SignedHash = bytes ]
=>
SMUID.SyncSession [ see SyncSession definition in 5.2.3.2]
*/
prepare_method(buffer, i, dev, SMUID, METHOD_START_SESSION_UID);
{
tiny_atom(buffer, i, 0, host_session_id);
short_atom(buffer, i, 1, 0, spid, spid_len);
tiny_atom(buffer, i, 0, write_session);
if (host_challenge) {
start_name(buffer, i);
tiny_atom(buffer, i, 0, 0);
medium_atom(buffer, i, 1, 0, host_challenge, host_challenge_len);
end_name(buffer, i);
}
if (host_signing_authority) {
start_name(buffer, i);
tiny_atom(buffer, i, 0, 3);
short_atom(buffer, i, 1, 0, host_signing_authority, host_signing_authority_len);
end_name(buffer, i);
}
}
finish_method(buffer, i);
return 0;
}
int start_session(struct disk_device *dev, const unsigned char *SPID, size_t user_id, const unsigned char *challenge,
size_t challenge_len)
{
int err = 0;
LOG(INFO, "------- START SESSION -------\n");
unsigned char buffer[2048] = { 0 };
size_t i = 0;
do_level_0_discovery(dev);
LOG(INFO, "base_com_id=%x user_id=%zu\n", dev->base_com_id, user_id);
if (user_id < SPECIAL_BASE_ID) {
unsigned char signing_auth[8];
memcpy(signing_auth, AUTHORITY_XXXX_UID, 8);
hex_add(signing_auth, 8, user_id);
generate_start_session_method(dev, buffer, &i, SPID, 8,
challenge, challenge_len, signing_auth, 8);
} else if (user_id == ANYBODY_USER_ID) {
generate_start_session_method(dev, buffer, &i, SPID, 8,
NULL, 0, NULL, 0);
} else if (user_id == SID_USER_ID) {
generate_start_session_method(dev, buffer, &i, SPID, 8,
challenge, challenge_len, AUTHORITY_SID_UID, 8);
} else if (user_id == PSID_USER_ID) {
generate_start_session_method(dev, buffer, &i, SPID, 8,
challenge, challenge_len, AUTHORITY_PSID_UID, 8);
} else {
LOG(ERROR, "Invalid user id.\n");
return err;
}
if ((err = invoke_method(dev, buffer, i, buffer, sizeof(buffer)))) {
LOG(ERROR, "Failed to start a session.\n");
return err;
}
/*
SMUID.SyncSession [
HostSessionID : uinteger,
SPSessionID : uinteger,
SPChallenge = bytes,
SPExchangeCert = bytes,
SPSigningCert = bytes,
TransTimeout = uinteger,
InitialCredit = uinteger,
SignedHash = bytes ]
*/
if ((err = skip_to_parameter(buffer, &i, 0, 0))) {
return err;
}
dev->host_session_id = be32_to_cpu(parse_int(buffer, &i));
dev->sp_session_id = be32_to_cpu(parse_int(buffer, &i));
LOG(INFO, "Created session with HostSessionID 0x%x and SPSessionID 0x%x.\n",
dev->host_session_id, dev->sp_session_id);
return err;
}
int close_session(struct disk_device *dev)
{
int err = 0;
unsigned char buffer[512] = { 0 };
size_t i = 0;
if (dev->host_session_id == 0 && dev->sp_session_id == 0) {
return 0;
}
prepare_headers(buffer, &i, dev);
end_session_token(buffer, &i);
finish_headers(buffer, &i);
if ((err = invoke_method(dev, buffer, i, buffer, sizeof(buffer)))) {
LOG(ERROR, "Failed to close session with HostSessionID 0x%x and SPSessionID 0x%x\n",
dev->host_session_id, dev->sp_session_id);
}
dev->sp_session_id = 0;
dev->host_session_id = 0;
LOG(INFO, "close_session err: %i\n", err);
LOG(INFO, "------- CLOSE SESSION -------\n\n");
return err;
}
int parse_bytes(const unsigned char *src, size_t *offset, unsigned char *dst, size_t dst_size, size_t *written)
{
size_t len = 0;
size_t i = 0;
if (offset) {
i = *offset;
}
if ((src[i] & 0b10000000) == 0b00000000) {
// tiny atom does not have byte type
return 1;
} else if ((src[i] & 0b11000000) == 0b10000000) {
// short atom
if ((src[i] & 0b00110000) != 0b00100000) {
// continuous or not byte type
return 1;
}
len = src[i] & 0b00001111;
i += 1;
} else if ((src[i] & 0b11100000) == 0b11000000) {
// medium atom
if ((src[i] & 0b00011000) != 0b00010000) {
// continuous or not byte type
return 1;
}
len = ((src[i + 0] & 0b00000111) << 8) | ((src[i + 1]) << 0);
i += 2;
} else if ((src[i] & 0b11110000) == 0b11100000) {
// long atom
if ((src[i] & 0b00000011) != 0b00000010) {
// continuous or not byte type
return 1;
}
len = ((src[i + 0]) << 16) | ((src[i + 1]) << 8) | ((src[i + 2]) << 0);
i += 3;
} else {
return 1;
}
if (dst) {
if (len > dst_size) {
return 1;
}
for (size_t j = 0; j < len; ++j) {
dst[j] = src[i + j];
}
}
i += len;
if (offset) {
*offset = i;
}
if (written) {
*written = len;
}
return 0;
}
int skip_atom(const unsigned char *src, size_t *offset, size_t total_length)
{
if ((src[*offset] & 0b10000000) == 0b00000000) {
// tiny atom
*offset += 1;
} else if ((src[*offset] & 0b11000000) == 0b10000000) {
// short atom
*offset += 1 + (src[*offset] & 0b00001111);
} else if ((src[*offset] & 0b11100000) == 0b11000000) {
// medium atom
*offset += 2 + (((src[*offset + 0] & 0b00000111) << 8) | ((src[*offset + 1]) << 0));
} else if ((src[*offset] & 0b11110000) == 0b11100000) {
// long atom
*offset += 3 + (((src[*offset + 0]) << 16) | ((src[*offset + 1]) << 8) | ((src[*offset + 2]) << 0));
} else if (src[*offset] == START_LIST_TOKEN) {
*offset += 1;
while (src[*offset] != END_LIST_TOKEN) {
skip_atom(src, offset, total_length);
}
*offset += 1;
} else if (src[*offset] == END_LIST_TOKEN) {
*offset += 1;
} else if (src[*offset] == START_NAME_TOKEN) {
*offset += 1;
skip_atom(src, offset, total_length);
skip_atom(src, offset, total_length);
if (src[*offset] != END_NAME_TOKEN) {
return 1;
}
*offset += 1;
} else if (src[*offset] == END_NAME_TOKEN) {
*offset += 1;
}
return 0;
}
int skip_to_parameter(unsigned char *src, size_t *offset, int parameter, int skip_mandatory)
{
struct packet_headers *headers = (struct packet_headers *)src;
size_t len = be32_to_cpu(headers->data_subpacket.length) + sizeof(struct packet_headers);
*offset = sizeof(struct packet_headers);
if (src[*offset] == START_LIST_TOKEN) {
*offset += 1;
} else if (src[*offset] == CALL_TOKEN) {
*offset += 20;
} else {
return 1;
}
if (!skip_mandatory) {
// getting mandatory argument
if (*offset >= len) {
return 1;
}
// skip previous arguments
for (int j = 0; j < parameter - 1; ++j) {
skip_atom(src, offset, len);
}
return 0;
} else {
// getting optional argument
// skip all mandatory arguments
for (int j = 0; j < skip_mandatory; ++j) {
skip_atom(src, offset, len);
}
while (*offset < len) {
if (*offset >= len || src[*offset] != START_NAME_TOKEN) {
return 1;
}
size_t tmp = *offset + 1;
uint64_t parsed_index = parse_int(src, &tmp);
if (parsed_index == (uint64_t)parameter) {
return 0;
}
skip_atom(src, offset, len);
}
}
return 1;
}
int set_row(struct disk_device *dev, const unsigned char *object_uid, unsigned char column, unsigned char *atom, size_t atom_len)
{
/*
ObjectUID.Set [
Values = typeOr { Bytes : bytes, RowValues : list [ ColumnNumber = <type of
column> ... ] } ]
=>
[ ]
*/
int err = 0;
unsigned char buffer[2048] = { 0 };
size_t i = 0;
prepare_method(buffer, &i, dev, object_uid, METHOD_SET_UID);
{
start_name(buffer, &i);
tiny_atom(buffer, &i, 0, METHOD_SET_PARAMETER_VALUES);
start_list(buffer, &i);
{
start_name(buffer, &i);
tiny_atom(buffer, &i, 0, column);
memcpy(buffer + i, atom, atom_len);
i += atom_len;
end_name(buffer, &i);
}
end_list(buffer, &i);
end_name(buffer, &i);
}
finish_method(buffer, &i);
if ((err = invoke_method(dev, buffer, i, buffer, sizeof(buffer)))) {
close_session(dev);
return err;
}
return err;
}
int get_row_bytes(struct disk_device *dev, const unsigned char *object_uid, unsigned char column, unsigned char *output,
size_t output_len, size_t *output_written)
{
/*