Debian Codegraph @

examples/gsm_directory.pl

raw symbols SCIP
1 #!/usr/bin/perl
2
3
4 # This program reads the phone directory of a GSM11.11 SIM card and
5 # prints out its contents in a human readable format. It is based on
6 # another Perl script from Ludovic ROUSSEAU <[email protected]>
7 # That you can obtain at the following URL:
8 # http://ludovic.rousseau.free.fr/softwares/SIM-1.0.tar.gz
9 #
10 # Copyright (C) 2001 - Lionel VICTOR, 2003 Ludovic ROUSSEAU
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26 use warnings;
27 use strict;
28 use Carp;
29
30 use Chipcard::PCSC;
31 use Chipcard::PCSC::Card;
32
33 use Getopt::Std;
34 use Term::ReadKey;
35
36 my %options;
37
38 my $hContext = new Chipcard::PCSC();
39 my $hCard;
40 my $RecvData;
41 my $nRecordsLength;
42 my $PIN;
43
44 die ("Could not create PCSC object: $Chipcard::PCSC::errno\n") unless defined $hContext;
45
46 sub gsmrecord_to_ascii {
47 my $gsmrecord = shift;
48 my $out_string = '';
49 my $delta = $#$gsmrecord-27;
50 my $i;
51
52 if ($$gsmrecord[0] != 0xFF) {
53 for $i (0..14+$delta-1) {
54 if ($$gsmrecord[$i] != 0xFF) {
55 $out_string.=chr($$gsmrecord[$i])
56 } else {
57 $out_string.=' ';
58 }
59 }
60
61 my $phone_length = $$gsmrecord[14] - 2;
62 if ($phone_length > 0) {
63 my $digit;
64 $out_string .= '-> ';
65 for $i (16+$delta..$#$gsmrecord) {
66 $digit = $$gsmrecord[$i]&0x0F;
67 $out_string .= sprintf ("%01X", $digit) if ($digit!=0x0F);
68 $digit = ($$gsmrecord[$i]&0xF0)>>4;
69 $out_string .= sprintf ("%01X", $digit) if ($digit!=0x0F);
70 }
71 } else {
72 $out_string.= "null or invalid phone";
73 }
74 }
75
76 return $out_string;
77 }
78
79 sub pin_to_array {
80 my $pin = shift;
81 my @array;
82
83 confess ("PIN code must not exceed 8 bytes.") unless (length $pin <= 8);
84
85 $pin =~ s/(.)/$1 /g;
86 @array = split / /, $pin;
87 @array = map (ord, @array);
88
89 # Pad the array with 0xFF up to 8 bytes
90 while ($#array < 7) { push @array, 0xFF; }
91
92 return @array;
93 }
94
95 getopt ('hvr:', \%options);
96
97 if (exists $options{h}) {
98 #TODO
99 print "TODO: usage -v verbose -r reader -h help";
100 }
101
102 if (exists $options{r}) {
103 $hCard = new Chipcard::PCSC::Card ($hContext, $options{r});
104 die ("Can't allocate PCSCCard object: $Chipcard::PCSC::errno\n") unless defined $hCard;
105 print STDERR "Using given card reader: $options{r}\n" if exists $options{'v'};
106 } else {
107 my @readers_list = $hContext->ListReaders ();
108 die ("Can't get readers list: $Chipcard::PCSC::errno\n") unless defined $readers_list[0];
109 print STDERR "No reader given: using $readers_list[0]\n" if exists $options{v};
110 $hCard = new Chipcard::PCSC::Card ($hContext, $readers_list[0]);
111 die ("Can't allocate PCSCCard object: $Chipcard::PCSC::errno\n") unless defined $hCard;
112 }
113
114 # Select MF (3F00)
115 print STDERR "Selecting Master File (3F00)\n" if exists $options{v};
116 $RecvData = $hCard->Transmit([0xA0, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00]);
117 die ("Can't communicate: $Chipcard::PCSC::errno\n") unless defined $RecvData;
118 die ("Can't select MF: SW = [ ".Chipcard::PCSC::array_to_ascii($RecvData)." ]") unless $$RecvData[0] == 0x9F;
119
120 # Select DF Telecom (7F10)
121 print STDERR "Selecting Telecom Directory (3F00/7F10)\n" if exists $options{v};
122 $RecvData = $hCard->Transmit([0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10]);
123 die ("Can't communicate: $Chipcard::PCSC::errno\n") unless defined $RecvData;
124 die ("Can't select DF Telecom: SW = [ ".Chipcard::PCSC::array_to_ascii($RecvData)." ]") unless $$RecvData[0] == 0x9F;
125
126 # Select EF_ADN (6F3A) (Abbreviated Dialing Numbers)
127 print STDERR "Selecting Abbreviated Dialing Numbers File (3F00/7F10/6F3A)\n" if exists $options{v};
128 $RecvData = $hCard->Transmit([0xA0, 0xA4, 0x00, 0x00, 0x02, 0x6F, 0x3A]);
129 die ("Can't communicate: $Chipcard::PCSC::errno\n") unless defined $RecvData;
130 die ("Can't select EF_ADN: SW = [ ".Chipcard::PCSC::array_to_ascii($RecvData)." ]") unless $$RecvData[0] == 0x9F;
131
132 # Get Response (get informations about EF_ADN)
133 # The last SW gives the length of available bytes:
134 # 9F xx means there are xx bytes waiting
135 print STDERR "Getting ADN Records length\n" if exists $options{v};
136 $nRecordsLength = $$RecvData[1];
137 $RecvData = $hCard->Transmit([0xA0, 0xC0, 0x00, 0x00, $nRecordsLength]);
138 die ("Can't communicate: $Chipcard::PCSC::errno\n") unless defined $RecvData;
139 die ("Can't retrieve records length: SW = [ ".Chipcard::PCSC::array_to_ascii($RecvData)." ]") unless $$RecvData[$nRecordsLength] == 0x90;
140
141 # Extract record length from the response
142 $nRecordsLength = $$RecvData[$nRecordsLength-1];
143 print STDERR "Records are $nRecordsLength bytes long\n" if exists $options{v};
144
145 # Asks the user for his/her PIN code
146 print STDOUT "Please insert your PIN code:";
147 ReadMode 'noecho';
148 $PIN = ReadLine 0;
149 ReadMode 'normal';
150 chomp $PIN;
151 print STDOUT "\n";
152
153 # Submitting PIN code to the card
154 print STDERR "Submitting PIN code\n" if exists $options{v};
155 $RecvData = $hCard->Transmit([0xA0, 0x20, 0x00, 0x01, 0x08, pin_to_array($PIN)]);
156 die ("Can't communicate: $Chipcard::PCSC::errno\n") unless defined $RecvData;
157 die ("Can't access file (wrong PIN code ?): SW = [ ".Chipcard::PCSC::array_to_ascii($RecvData)." ]") unless $$RecvData[0] == 0x90;
158
159 # Get phone records
160 my $i;
161 my @SW;
162 print STDERR "Getting records\n" if exists $options{v};
163 for $i (1..255) {
164 $RecvData = $hCard->Transmit([0xA0, 0xB2, $i, 0x04, $nRecordsLength]);
165 die ("Can't communicate: $Chipcard::PCSC::errno\n") unless defined $RecvData;
166
167 # pop the status word out of the retrieved bytes
168 $SW[1] = pop @$RecvData;
169 $SW[0] = pop @$RecvData;
170
171 # Ignore referencing errors (record out of range, file
172 # empty, etc...) but die if anything else bad occurs.
173 die ("Can't read from EF_ADN: SW = [ ".sprintf ("%02X %02X",$SW[0],$SW[1])." ]\n") if ($SW[0] != 0x94 && $SW[0] != 0x90);
174
175 # Exit if the status word is not 90xx
176 last if $SW[0] != 0x90;
177
178 # TODO:remove that it is only debug...
179 # TODO: format that and print phone record...
180 print "SLOT $i: ".gsmrecord_to_ascii($RecvData)."\n";
181 #print "[ ".Chipcard::PCSC::array_to_ascii($RecvData)." ] [".Chipcard::PCSC::array_to_ascii(\@SW)."]\n";
182 }
183
184 print STDERR "Reseting the card\n" if exists $options{v};
185 $hCard->Disconnect($Chipcard::PCSC::SCARD_RESET);
186
187 # End of File
188