-
Notifications
You must be signed in to change notification settings - Fork 7
/
simparser.pl
executable file
·1626 lines (1543 loc) · 52.7 KB
/
simparser.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use Carp;
# Environment variables:
# FORCE_USIM - don't consider input to be GSM
# FORCE_GSM - don't consider input to be USIM
while(<>) {
# header
print;
last if(/^\s*$/);
}
my $GSMSTART = ['A0', 'A4', '00'];
my $USIMSTART = ['00', 'A4', '00'];
my @SIM_SERVICES = (
"CHV1 disable function",
"Abbreviated Dialling Numbers",
"Fixed Dialling Numbers",
"Short Message Storage",
"Advice of Charge",
"Capability Configuration Parameters",
"PLMN selector",
"(RFU)",
"MSISDN",
"Extension1",
"Extension2",
"SMS Parameters",
"Last Number Dialled",
"Cell Broadcast Message Identifier",
"Group Identifier Level 1",
"Group Identifier Level 2",
"Service Provider Name",
"Service Dialling Numbers",
"Extension3",
"(RFU)",
"VCGS Group Identifier List",
"VBS Group Identifier List",
"enhanced Multi-Level Precedence and Pre-emption Service",
"Automatic Answer for eMLPP",
"Data download via SMS-CB",
"Data download via SMS-PP",
"Menu selection",
"Call control",
"Proactive SIM",
"Cell Broadcast Message Identifier Ranges",
"(RFU)",
"(RFU)",
);
# 3GPP TS 102.223 v10.05.00, pg 23
my @ME_SERVICES = (
"Profile download", # start of byte 1
"SMS-PP data download",
"Cell Broadcast data download",
"Menu selection",
"SMS-PP data download",
"Timer expiration",
"USSD string data object support in Call Control by USIM",
"Call Control by NAA",
"Command result", # start of byte 2
"Call Control by NAA",
"Call Control by NAA",
"MO short message control support",
"Call Control by NAA",
"UCS2 Entry supported",
"UCS2 Display supported",
"Display Text",
"Proactive SIM: DISPLAY TEXT", # start of byte 3
"Proactive SIM: GET INKEY",
"Proactive SIM: GET INPUT",
"Proactive SIM: MORE TIME",
"Proactive SIM: PLAY TONE",
"Proactive SIM: POLL INTERVAL",
"Proactive SIM: POLLING OFF",
"Proactive SIM: REFRESH",
"Proactive SIM: SELECT ITEM", # start of byte 4
"Proactive SIM: SEND SHORT MESSAGE",
"Proactive SIM: SEND SS",
"Proactive SIM: SEND USSD",
"Proactive SIM: SET UP CALL",
"Proactive SIM: SET UP MENU",
"Proactive SIM: PROVIDE LOCAL INFORMATION (MCC, MNC, LAC, Cell ID & IMEI)",
"Proactive SIM: PROVIDE LOCAL INFORMATION (NMR)",
"Proactive SIM: SET UP EVENT LIST", # start of byte 5
"Event: MT call",
"Event: Call connected",
"Event: Call disconnected",
"Event: Location status",
"Event: User activity",
"Event: Idle screen available",
"Event: Card reader status",
"Event: Language selection", # start of byte 6
"Event: Browser Termination",
"Event: Data available",
"Event: Channel status",
"Event: Access Technology Change",
"Event: Display parameters changed",
"Event: Local Connection",
"Event: Network Search Mode Change",
"Proactive UICC: POWER ON CARD", # start of byte 7
"Proactive UICC: POWER OFF CARD",
"Proactive UICC: PERFORM CARD APDU",
"Proactive UICC: GET READER STATUS (Card reader status)",
"Proactive UICC: GET READER STATUS (Card reader identifier)",
"(RFU)",
"(RFU)",
"(RFU)",
"Proactive UICC: TIMER MANAGEMENT (start, stop)", # start of byte 8
"Proactive UICC: TIMER MANAGEMENT (get current value)",
"Proactive UICC: PROVIDE LOCAL INFORMATION (date, time and time zone)",
"GET INKEY",
"SET UP IDLE MODE TEXT",
"RUN AT COMMAND",
"SETUP CALL",
"Call Control by NAA",
"DISPLAY TEXT", # start of byte 9
"SEND DTMP command",
"Proactive UICC: PROVIDE LOCAL INFORMATION (NMR)",
"Proactive UICC: PROVIDE LOCAL INFORMATION (language)",
"Proactive UICC: PROVIDE LOCAL INFORMATION (Timing Advance)",
"Proactive UICC: LANGUAGE NOTIFICATION",
"Proactive UICC: LAUNCH BROWSER",
"Proactive UICC: PROVIDE LOCAL INFORMATION (Access Technology)",
"Soft keys support for SELECT ITEM", # start of byte 10
"Soft keys support for SET UP MENU",
"(RFU)",
"(RFU)",
"(RFU)",
"(RFU)",
"(RFU)",
"(RFU)",
"(Soft keys information)", # start of byte 11
"(Soft keys information)",
"(Soft keys information)",
"(Soft keys information)",
"(Soft keys information)",
"(Soft keys information)",
"(Soft keys information)",
"(Soft keys information)",
"Proactive UICC: OPEN CHANNEL", # start of byte 12
"Proactive UICC: CLOSE CHANNEL",
"Proactive UICC: RECEIVE DATA",
"Proactive UICC: SEND DATA",
"Proactive UICC: GET CHANNEL STATUS",
"Proactive UICC: SERVICE SEARCH",
"Proactive UICC: GET SERVICE INFORMATION",
"Proactive UICC: DECLARE SERVICE",
"CSD", # start of byte 13
"GPRS",
"Bluetooth",
"IrDA",
"RS232",
"(Number of channels supported)",
"(Number of channels supported)",
"(Number of channels supported)",
"(Screen height)", # start of byte 14
"(Screen height)",
"(Screen height)",
"(Screen height)",
"(Screen height)",
"No display capability",
"No keypad capability",
"Screen Sizing Parameters",
"(Screen width)", # start of byte 15
"(Screen width)",
"(Screen width)",
"(Screen width)",
"(Screen width)",
"(Screen width)",
"(Screen width)",
"Variable size fonts",
"Display can be resized", # start of byte 16
"Text Wrapping supported",
"Text Scrolling supported",
"Text Attributes supported",
"(RFU)",
"(Width reduction)",
"(Width reduction)",
"(Width reduction)",
"TCP, UICC in client mode, remote connection", # start of byte 17
"UDP, UICC in client mode, remote connection",
"TCP, UICC in server mode, remote connection",
"TCP, UICC in client mode, local connection",
"UDP, UICC in client mode, local connection",
"Direct communication channel",
"E-UTRAN",
"HSDPA",
"Proactive UICC: DISPLAY TEXT (Variable Time out)", # start of byte 18
"Proactive UICC: GET INKEY (help is supported)",
"USB (Bearer Independent protocol supported bearers)",
"Proactive UICC: GET INKEY (Variable Timeout)",
"Proactive UICC: PROVIDE LOCAL INFORMATION (ESN)",
"Call control on GPRS",
"Proactive UICC: PROVIDE LOCAL INFORMATION (IMEISV)",
"Proactive UICC: PROVIDE LOCAL INFORMATION (Search Mode change)",
"(Reserved by TIA/EIA-136)", # start of byte 19
"(Reserved by TIA/EIA-136)",
"(Reserved by TIA/EIA-136)",
"(Reserved by TIA/EIA-136)",
"(RFU)",
"(RFU)",
"(RFU)",
"(RFU)",
"(Reserved by TIA/EIA/IS-820)", # start of byte 20
"(Reserved by TIA/EIA/IS-820)",
"(Reserved by TIA/EIA/IS-820)",
"(Reserved by TIA/EIA/IS-820)",
"(Reserved by TIA/EIA/IS-820)",
"(Reserved by TIA/EIA/IS-820)",
"(Reserved by TIA/EIA/IS-820)",
"(Reserved by TIA/EIA/IS-820)",
"**End of list** more options still remain in the standard"
);
# 3GPP TS 101.221 pg 80
my @SELECT_SIM_TAGS = (
"File size",
"Total file size",
"File descriptor",
"File identifier",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"Short file identifier",
"(invalid)",
"Life cycle status integer",
"Security attributes type 1",
"Security attributes type 2",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"Proprietary information",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"(invalid)",
"Security attributes",
);
# ETSI TS 101.220 v11.0.0 (pg 14)
my @PROACTIVE_SIM_TAGS = (
"(invalid)", # 0
"Command details tag",
"Device identity tag",
"Result tag",
"Duration tag",
"Alpha identifier tag",
"Address tag",
"Capability configuration parameters tag",
"Called party subaddress tag",
"SS string tag",
"Reserved for USSD string tag", # 10
"SMS TPDU tag",
"Cell Broadcast page tag",
"Text string tag",
"Tone tag / eCAT client profile",
"Item tag / eCAT client identity",
"Item identifier tag / Encapsulated envelope type",
"Response length tag",
"File List tag",
"Location Information tag",
"IMEI tag", # 20
"Help request tag",
"Network Measurement Results tag",
"Default Text tag",
"Items Next Action Indicator tag",
"Event list tag",
"Cause tag (reserved)",
"Location status tag",
"Transaction identifier tag",
"BCCH channel list tag (reserved)",
"Icon identifier tag", # 30
"Item Icon identifier list tag",
"Card reader status tag",
"Card ATR tag",
"C-APDU tag",
"R-APDU tag",
"Timer identifier tag",
"Timer value tag",
"Date-Time and Time zone tag",
"Call control requested action tag",
"AT Command tag", # 40
"AT Response tag",
"BC Repeat Indicator tag (reserved)",
"Immediate response tag",
"DTMF string tag",
"Language tag",
"Timing Advance tag (reserved)",
"AID tag",
"Browser Identity tag",
"URL / URI tag",
"Bearer tag", # 50
"Provisioning Reference File tag",
"Browser Termination Clause tag",
"Bearer description tag",
"Channel data tag",
"Channel data length tag",
"Channel status tag",
"Buffer size tag",
"Card reader identification tag",
"File Update Information tag",
"UICC/terminal interface transport level tag", # 60
"Not used",
"Other address (data destination address) tag",
"Access Technology tag",
"Display parameters tag",
"Service Record tag",
"Device Filter tag",
"Service Search tag",
"Attribute information tag",
"Service Availability tag",
"ESN tag (reserved)", # 70
"Network Access Name tag",
"CDMA-SMS-TPDU tag (reserved)",
"Remote Entity Address tag",
"I-WLAN identifier tag (reserved)",
"I-WLAN Access Status tag (reserved)",
"Reserved for future use",
"Reserved for future use",
"Reserved for future use",
"Reserved for future use",
"Text attribute tag", # 80
"Item text attribute tag",
"PDP context activation tag (reserved)",
"Contactless state request tag",
"Contactless functionality state tag",
"CSG cell selection status (reserved)",
"CSG ID (reserved)",
"HNB name (reserved)",
"Reserved for future use",
"Reserved for future use",
"Reserved for future use", # 90
"Reserved for future use",
"Reserved for future use",
"Reserved for future use",
"Reserved for future use",
"Reserved for future use",
"Reserved for future use",
"Reserved for future use",
"IMEISV tag",
"Battery state tag",
"Browsing status tag", # 100
"Network Search Mode tag",
"Frame Layout tag",
"Frames Information tag",
"Frame identifier tag",
"UTRAN/E-UTRAN Measurement Qualifier tag (reserved)",
"Multimedia Message Reference tag",
"Multimedia Message Identifier tag",
"Multimedia Message Transfer Status tag",
"MEID tag",
"Multimedia Message Content Identifier tag",
"Multimedia Message Notification tag",
"Last Envelope tag",
"Registry application data tag",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag reserved for 3GPP",
"Tag 7F / FF; end of tag list; invalid tag"
);
my %DEVICE_IDENTIFIERS = (
'01' => "Keypad",
'02' => "Display",
'03' => "Earpiece",
'81' => "SIM",
'82' => "ME",
'83' => "Network",
);
# GSM TS 11.14 says the commands are listed in section 13.4
# however, section 13.4 does not exist (section 13 is very short and has no subsections)
# alternative source, table 1, chapter 1, page 6:
# http://www.multitech.com/en_US/documents/collateral/manuals/s000391c.pdf
my %PROACTIVE_COMMAND_DESCRIPTION = (
# Any commented out value comes from the linked document, but isn't named in GSM TS 11.14
'01' => "REFRESH",
'03' => "POLL INTERVAL", # <-- unsure, but very likely (parameters match)
# '05' => "SET UP EVENT LIST",
# Possibilities, considering:
# - Only device identity, then unimplemented or no parameters
# - All other values are already defined
# - Device identity is from SIM to ME
# are:
# MORE TIME, POLLING OFF, PROVIDE LOCAL INFORMATION
'10' => "SET UP CALL",
'11' => "SEND SS",
# '12' => "SEND USSD",
'13' => "SEND SHORT MESSAGE",
# '14' => "SEND DTMF",
# '15' => "LAUNCH BROWSER",
'20' => "PLAY TONE",
'21' => "DISPLAY TEXT",
'22' => "GET INKEY",
'23' => "GET INPUT",
'24' => "SELECT ITEM",
'25' => "SET UP MENU",
# '28' => "SET UP IDLE MODE TEXT",
# Not listed:
# MORE TIME
# POLLING OFF
# PROVIDE LOCAL INFORMATION
);
my %RESULT_TAG_DESCRIPTION = (
'00' => "Command performed succesfully",
'01' => "Command performed with partial comprehension",
'02' => "Command performed, with missing information",
'03' => "REFRESH performed with additional EFs read",
'10' => "Proactive SIM session terminated by user",
'11' => "Backward move in the proactive SIM session requested by the user",
'12' => "No response from user",
'20' => "ME currently unable to process command",
'21' => "Network currently unable to process command",
'22' => "User did not accept call set-up request",
'23' => "User cleared down call before connection or network release",
'30' => "Command beyond ME's capabilities",
'31' => "Command type not understood by ME",
'32' => "Command data not understood by ME",
'33' => "Command number not known by ME",
'34' => "SS Return Error",
'35' => "SMS RP-ERROR",
'36' => "Error, required values are missing",
# Missing: Screen is busy, Currently busy on call, No service, Busy on SS transaction
);
my $startsec;
my @unparsed_bytes;
my $gsmstarted = 0;
my @warnings;
my $relsec = 0;
my $cmdstartsec;
my $is_usim = 0;
my $maybe_not_usim = 0;
my $apdus = 0;
while(<>) {
s/[\r\n]+//g;
next if($_ =~ /^$/);
my ($hour, $min, $sec, $msec) = /^(\d\d):(\d\d):(\d\d)\.(\d\d\d):$/;
next if !defined $msec;
$sec = $hour * 3600 + $min * 60 + $sec + ($msec / 1000);
if(!defined($startsec)) {
$startsec = $sec;
$relsec = 0;
} else {
$relsec = $sec - $startsec;
}
$_ = <>;
if(isOdd(length $_)) {
die "Bytes don't have even length: $_\n";
}
push @unparsed_bytes, $_ =~ /(..)/g;
if(!$gsmstarted) {
# find GSMSTART in unparsed_bytes
my $pos = $ENV{FORCE_USIM} ? -1 : findBytes($GSMSTART, \@unparsed_bytes);
my $upos = $ENV{FORCE_GSM} ? -1 : findBytes($USIMSTART, \@unparsed_bytes);
if($pos >= 0 || $upos >= 0) {
# Read from USIM start if it's found earlier
$is_usim = $upos >= 0 && ($pos < 0 || $upos < $pos);
# Though, fallback to GSM later if SIM card did not understand USIM
$maybe_not_usim = $is_usim;
$pos = $upos if($is_usim);
$gsmstarted = 1;
print "$relsec: found " . ($is_usim ? "USIM" : "GSM") . " communication start\n";
my @start_bytes = splice @unparsed_bytes, 0, $pos if $pos > 0;
my %unique_bytes;
foreach(@start_bytes) {
$unique_bytes{$_} ||= 0;
$unique_bytes{$_} ++;
}
print "$relsec: Bytes before communication start: " . join(', ', keys %unique_bytes) . "\n";
} else {
next;
}
}
# Try to parse a command and response APDU
# ETSI TS 102.221 (USIM) calls orig_len "p3", the first five bytes the "command header"
# in old SIM, the card always responds with INS again, then either of both sends some data
# in USIM, the UICC will only send a procedure byte if the length is not 0x00.
# * Equal to INS byte => terminal/uicc should continue reading/writing data as planned
# * Equal to complement of INS => terminal/uicc should send next byte
# * 0x60 ("NULL") => wait a bit for the next procedure byte
# * SW1 (only 0x61 or 0x6c) => wait for SW2, then send GET RESPONSE or repeat the command
my ($cla, $ins, $p1, $p2, $orig_len, $proc_byte) = @unparsed_bytes;
$cmdstartsec = $relsec if(defined $cla && !defined($cmdstartsec));
next if(!defined($proc_byte));
my $len = hex($orig_len);
my $sending = insIsSending($cla, $ins);
# Only require length to be > 0 if this is USIM
if(!$is_usim || $len > 0) {
if($ins ne $proc_byte) {
if($proc_byte eq "6C") {
my (undef, undef, undef, undef, undef, undef, $sw2) = splice @unparsed_bytes, 0, 7;
warning("SIM tells phone its length is incorrect; try again with 0x$sw2 (".hex($sw2).").");
next;
} elsif($proc_byte eq "6A") {
# This is a normal situation if the phone tries to speak USIM to the card and
# the card only understands GSM
warning("SIM tells phone P1 or P2 is incorrect");
$len = 0;
# fall-through
} else {
warning("Procedure byte is not equal to INS; INS=$ins, RESP_INS=$proc_byte");
}
}
}
if(!defined($sending)) {
my $pos = findBytes(["A0"], \@unparsed_bytes, 1);
my $upos = findBytes(["00"], \@unparsed_bytes, 1);
if($pos < 0 && $upos < 0) {
warning("Command not understood or SIM card responded incorrectly... waiting for new command.");
next;
}
my $this_is_usim = 0;
if($is_usim && $upos >= 0 && ($pos < 0 || $upos < $pos)) {
$this_is_usim = 1;
$pos = $upos;
}
my @skipped_bytes = splice @unparsed_bytes, 0, $pos if $pos > 0;
printf("%.3f: Bytes before communication restoration:\n", $relsec);
print printapdu(\@skipped_bytes) . "\n";
warning("Command not understood or SIM card responded incorrectly, skipped $pos bytes to next " . ($this_is_usim ? "USIM" : "GSM") . " command!");
print "\n";
next;
}
if(!$sending && $len != 0) {
warning("Command APDU describes length, but this INS byte is known not to send data");
} elsif($sending eq "SIM" && $len == 0) {
# GSM 11.11 Version 5.3.0 page 35
# If the SIM should send data, but length is 0x00, then
# there will be a data transfer of 256 bytes.
$len = 256 if(!$is_usim);
# In USIM, according to TS 102.221, this happens by sending procedure bytes (???)
# Not sure if that's true, it's not clear from the specs.
# See 7.3.1.1.5.1, page 47
}
my @payload = @unparsed_bytes[6 .. 5 + $len] if($len != 0);
next if(@payload != $len);
my ($sw1, $sw2);
# TODO: In usim mode, we should read procedure bytes. They come before the
# status words if the length is nonzero.
if($is_usim && $len == 0) {
($sw1, $sw2) = @unparsed_bytes[5..6];
} else {
($sw1, $sw2) = @unparsed_bytes[(6 + $len) .. (7 + $len)];
}
next if(!defined($sw2));
my $commandapdu = [$cla, $ins, $p1, $p2, $orig_len];
my $responseapdu = [$sw1, $sw2];
if($is_usim && $len == 0) {
splice @unparsed_bytes, 0, 7;
} else {
splice @unparsed_bytes, 0, 8 + $len;
}
my $explanations = explain($commandapdu, $responseapdu, \@payload, $sending);
print "Apdu " . ++$apdus . ": \n";
output("COMMAND", $cmdstartsec, printapdu($commandapdu), $explanations->[0]);
if($sending eq "ME") {
output("COMMAND DATA", undef, printapdu(\@payload), $explanations->[2]);
}
output("RESPONSE", $relsec, printapdu($responseapdu), $explanations->[1]);
if($sending eq "SIM") {
output("RESPONSE DATA", undef, printapdu(\@payload), $explanations->[2]);
}
print "\n";
undef $cmdstartsec;
}
printf "%.3f: Done. List of warnings during operation:\n", $relsec;
use Data::Dumper;
print Dumper(@warnings);
sub isOdd {
return ($_[0] % 2) == 1;
}
sub warning {
my $warning = sprintf("%.3f: %s", $relsec, $_[0]);
push @warnings, $warning;
warn "$warning\n";
}
sub printapdu {
return join ' ', @{$_[0]};
}
sub findBytes {
my ($bytes, $string, $offset) = @_;
$offset ||= 0;
OUTER: for(my $i = $offset; $i <= @$string - @$bytes; ++$i) {
INNER: for(my $j = 0; $j < @$bytes; ++$j) {
if($string->[$i + $j] ne $bytes->[$j]) {
next OUTER;
}
}
return $i;
}
return -1;
}
sub insIsSending {
my ($cla, $ins) = @_;
# Source: GSM 11.11 version 5.3.0 page 38
my @gsm_sending_bytes = qw/A4 D6 DC A2 32 20 24 26 28 2C 88 10 C2 14/;
my @gsm_receiving_bytes = qw/A4 F2 B0 B2 A2 32 88 C0 C2 12/;
my @gsm_nodata_bytes = qw/04 44 FA/;
# Source: ETSI TS 102.221 version 10.0.0 page 71
my $rcla = hex $cla;
my $valid_gsm_cla = !bit($rcla, 7) && !bit($rcla, 5);
if($valid_gsm_cla) {
if($ins ~~ @gsm_sending_bytes) {
return "ME";
} elsif($ins ~~ @gsm_receiving_bytes) {
return "SIM";
} elsif($ins ~~ @gsm_nodata_bytes) {
return "";
} else {
warning "Unrecognised instruction byte $ins (CLA=$cla).";
return;
}
} else {
warning "Unrecognised class byte $cla.";
return;
}
}
sub output {
my ($type, $sec, $string, $explanation) = @_;
if(defined $sec) {
$sec = sprintf("%.3f", $sec) . ":";
} else {
$sec = "";
}
printf "%8s %13s: %s\n", $sec, $type, $string;
print ((' ' x 24), $explanation, "\n") if $explanation;
}
my @selected_file;
my $stk_length;
my $response_type;
sub explain {
my ($command, $response, $data, $sending) = @_;
my ($cla, $ins, $p1, $p2, $len) = @$command;
my ($sw1, $sw2) = ($response->[-2], $response->[-1]);
# In USIM, any CLA is allowed, as long as bits 5 and 7 are 0
# ETSI TS 102.221 version 10.0.0 page 71
my $cla046 = !$is_usim || $cla =~ /^(0|4|6).$/;
my $cla8ce = !$is_usim || $cla =~ /^(8|c|e).$/i;
my $rcla = hex $cla;
die if(bit($rcla, 7) || bit($rcla, 5));
my ($cmd_explain, $resp_explain, $data_explain);
my $p1_understood = "00";
my $p2_understood = "00";
# Immediately forget response_type if the command is not GET RESPONSE
unless($ins eq "C0") {
undef $response_type;
}
if($ins eq "A4") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "SELECT file";
@selected_file = @$data;
$response_type = "SELECT";
warning "SELECT with data length is not 2, and this is not USIM" if @$data != 2 && !$is_usim;
my $is_aid = 0;
if($p1 eq "04") {
$p1_understood = $p1;
$is_aid = 1;
}
if($maybe_not_usim && $is_usim && $sw1 eq "6A" && $sw2 eq "86") {
$maybe_not_usim = 0;
$is_usim = 0;
$data_explain = "Phone understands USIM, but SIM does not; switching to GSM mode";
warning $data_explain;
} else {
$data_explain = getFileDescription($data, $is_aid);
}
} elsif($ins eq "F2") {
warning "Wrong class byte for this instruction" if !$cla8ce;
$cmd_explain = "STATUS";
if(!@selected_file) {
warning "STATUS command out of sequence?";
} else {
if($p2 eq "0C") {
$p2_understood = $p2;
$data_explain = "STATUS without description of file";
} elsif($p2 eq "01") {
$p2_understood = $p2;
$data_explain = "TLV object of currently selected application";
} else {
if(!@$data) {
$data_explain = "There should be data, but there isn't...";
} else {
my @lines = explain_status_file(@selected_file, $data);
$data_explain = "== Description of file ".join(' ', @selected_file)." ==\n";
foreach(@lines) {
$data_explain .= (' ' x 24) . $_ . "\n";
}
$data_explain =~ s/\n$//;
}
}
}
} elsif($ins eq "B0") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "READ BINARY";
my @lines = explain_file_contents($data, @selected_file);
$data_explain = "== Contents of file ".join(' ', @selected_file)." ==\n";
foreach(@lines) {
$data_explain .= (' ' x 24) . $_ . "\n";
}
$data_explain =~ s/\n$//;
} elsif($ins eq "D6") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "UPDATE BINARY";
} elsif($ins eq "B2") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "READ RECORD";
} elsif($ins eq "DC") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "UPDATE RECORD";
} elsif($ins eq "A2") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "SEEK / SEARCH RECORD";
} elsif($ins eq "32") {
warning "Wrong class byte for this instruction" if !$cla8ce;
$cmd_explain = "INCREASE";
} elsif($ins eq "CB") {
warning "Wrong class byte for this instruction" if !$cla8ce;
$cmd_explain = "RETRIEVE DATA"
} elsif($ins eq "DB") {
warning "Wrong class byte for this instruction" if !$cla8ce;
$cmd_explain = "SET DATA";
} elsif($ins eq "20") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "VERIFY CHV";
$p2_understood = $p2;
my $pin = bytes_to_string($data, 0xff);
$data_explain = "PIN code: \"$pin\"";
} elsif($ins eq "24") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "CHANGE CHV";
} elsif($ins eq "26") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "DISABLE CHV";
} elsif($ins eq "28") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "ENABLE CHV";
} elsif($ins eq "2C") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "UNBLOCK CHV";
} elsif($ins eq "04") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "INVALIDATE / DEACTIVATE FILE";
} elsif($ins eq "44") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "REHABILITATE / ACTIVATE FILE";
} elsif($ins eq "88" || $ins eq "89") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "RUN GSM ALGORITHM / AUTHENTICATE";
} elsif($ins eq "84") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "GET CHALLENGE";
} elsif($ins eq "FA") {
warning "The SLEEP instruction is obsolete";
$cmd_explain = "SLEEP";
} elsif($ins eq "AA") {
warning "Wrong class byte for this instruction" if !$cla8ce;
$cmd_explain = "TERMINAL CAPABILITY";
} elsif($ins eq "C0") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "GET RESPONSE, " . (hex $len) . " bytes";
if(!defined($response_type)) {
$data_explain = "(did not expect this GET RESPONSE)\n";
warning "Did not expect this GET RESPONSE.\n";
} elsif($response_type eq "SELECT") {
my $tag = $data->[0];
my $length = hex $data->[1];
my $offset = 2;
my $context = {};
$data_explain = "== Response to SELECT ==\n";
until($offset >= $length) {
my ($lastlength, @lines) = explain_data_object($data, $offset, $context, "SELECT");
$offset += $lastlength;
foreach(@lines) {
$data_explain .= (' ' x 24) . $_ . "\n";
}
}
$data_explain =~ s/\n$//;
}
} elsif($ins eq "10") {
warning "Wrong class byte for this instruction" if $is_usim and $cla ne "80";
$cmd_explain = "TERMINAL PROFILE";
my $offset = 1;
my @services_enabled;
foreach(@$data) {
my $byte = hex $_;
for(1..8) {
my $value = bit($byte, $_);
$services_enabled[$offset] = $value;
$offset ++;
}
}
$data_explain = "== Mobile Equipment SIM Toolkit Capabilities ==\n";
for(my $i = 1; $i < @services_enabled; ++$i) {
my $service = $ME_SERVICES[$i-1] || "(unknown capability)";
my $enabled = $services_enabled[$i] ? "enabled" : "not enabled";
$data_explain .= (' ' x 24) . "Capability $i: $service ($enabled)\n";
}
} elsif($ins eq "C2") {
warning "Wrong class byte for this instruction" if $is_usim and $cla ne "80";
$cmd_explain = "ENVELOPE (SMS Point-to-point or cell broadcast data download)";
my $tag = $data->[0];
my $descr = "???";
# TS 101.220 v11.00.00 p11 to 16
if($tag eq "CF") {
$descr = "Reserved for proprietary use";
# TODO: what would this do?
} elsif($tag eq "D0") {
warning "Proactive Command byte in ENVELOPE!";
$descr = "Proactive Command (forbidden in ENVELOPE!)";
} elsif($tag eq "D1") {
$descr = "SMS-PP download";
} elsif($tag eq "D2") {
$descr = "Cell Broadcast download";
} elsif($tag eq "D3") {
$descr = "Menu Selection";
} elsif($tag eq "D4") {
$descr = "Call Control";
} elsif($tag eq "D5") {
$descr = "MO Short Message Control";
} elsif($tag eq "D6") {
$descr = "Event Download";
} elsif($tag eq "D7") {
$descr = "Timer Expiration";
} elsif($tag eq "D8") {
$descr = "Reserved for intra-UICC communication";
# TODO: what would this do?
} elsif($tag eq "D9") {
$descr = "USSD Download";
} elsif($tag eq "DA") {
$descr = "MMS Transfer Status";
} elsif($tag eq "DB") {
$descr = "MMS notification download";
} elsif($tag eq "DC") {
$descr = "Terminal application tag";
} elsif($tag eq "DD") {
$descr = "Geographic Location Reporting tag";
}
my $length = hex $data->[1];
if($length != (hex $len) - 2) {
warning "Length of the ENVELOPE instruction seems incorrect ($length, but ".($len - 2) . " bytes left in response)";
}
my $offset = 2;
my $context = {};
$data_explain = "== Command from ME / network to SIM ==\n";
$data_explain .= (' ' x 24) . "Type: $tag ($descr)\n";
until($offset >= $length) {
my ($lastlength, @lines) = explain_data_object($data, $offset, $context);
$offset += $lastlength;
foreach(@lines) {
$data_explain .= (' ' x 24) . $_ . "\n";
}
}
$data_explain =~ s/\n$//;
undef $stk_length;
} elsif($ins eq "12") {
warning "Wrong class byte for this instruction" if $is_usim and $cla ne "80";
$cmd_explain = "FETCH";
# First byte should be Proactive SIM command tag
# Second (and according to specs, "maybe" third) should be length
# After that are multiple data blocks that can be explained by explain_data_object
if($data->[0] ne "D0") {
warning "First byte of FETCH command was not 'Proactive SIM command tag', this is required by BER-TLV in SIM to ME direction";
}
my $length = hex $data->[1];
if($length != (hex $len) - 2) {
warning "Length of the FETCH instruction seems incorrect ($length, but ".($len - 2) . " bytes left in response)";
}
my $offset = 2;
my $context = {};
$data_explain = "== Proactive SIM command from SimToolkit to phone ==\n";
until($offset >= $length) {
my ($lastlength, @lines) = explain_data_object($data, $offset, $context);
$offset += $lastlength;
foreach(@lines) {
$data_explain .= (' ' x 24) . $_ . "\n";
}
}
$data_explain =~ s/\n$//;
undef $stk_length;
} elsif($ins eq "14") {
warning "Wrong class byte for this instruction" if $is_usim and $cla ne "80";
$cmd_explain = "TERMINAL RESPONSE";
# GSM TS 11.14 section 6.8
# This response contains SIMPLE-TLV objects, three of which are mandatory,
# some of which are optional
my $read = 0;
my $context = {};
$data_explain = "== Response from phone to proactive SIM command from SimToolkit ==\n";
# TODO: check if command details, device identities and result are given
# all others are optional, maybe check if there is too much data too?
until($read == @$data) {
my ($lastlength, @lines) = explain_data_object($data, $read, $context);
$read += $lastlength;
foreach(@lines) {
$data_explain .= (' ' x 24) . $_ . "\n";
}
}
$data_explain =~ s/\n$//;
} elsif($ins eq "70") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "MANAGE CHANNEL";
} elsif($ins eq "73") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "MANAGE SECURE CHANNEL";
} elsif($ins eq "75") {
warning "Wrong class byte for this instruction" if !$cla046;
$cmd_explain = "TRANSACT DATA";
} else {
$cmd_explain = "???";
warning "Unknown GSM INS byte: $ins";
}
if($p1_understood ne $p1) {
$cmd_explain .= " [P0=$p1?]";
}
if($p2_understood ne $p2) {
$cmd_explain .= " [P1=$p2?]";
}
my $sw2h = hex $sw2;
if($sw1 eq "90" && $sw2 eq "00") {