Debian Codegraph @

Card/Card.pm

raw symbols SCIP
1 ###############################################################################
2 #
3 # Authors : Lionel VICTOR <[email protected]>
4 # <[email protected]>
5 # Ludovic ROUSSEAU <[email protected]>
6 # Compiler : gcc, Visual C++
7 # Target : Unix, Windows
8 #
9 # Description : Perl wrapper to the PCSC API
10 #
11 # Copyright (C) 2001 - Lionel VICTOR
12 # 2003-2008 - Ludovic ROUSSEAU
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License as
16 # published by the Free Software Foundation; either version 2 of the
17 # License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 # 02111-1307 USA
28 #
29 ###############################################################################
30
31 package Chipcard::PCSC::Card;
32
33 require Chipcard::PCSC;
34
35 use warnings;
36 use Carp;
37 use strict;
38
39 our $VERSION = '0.02';
40
41 # Usage:
42 # $hCard = new Chipcard::PCSC::Card ($hcontext);
43 # $hCard = new Chipcard::PCSC::Card ($hcontext, $reader_name, $share_mode,
44 # $preferred_protocol);
45 #
46 # the second version also connect to the supplied reader.
47 # when no connection has been required, use Connect()
48 #
49 # default values:
50 # $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
51 # $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1
52 sub new
53 {
54 my $class = shift;
55
56 my $hContext = shift;
57 my $reader_name = shift;
58 my $share_mode = shift;
59 my $preferred_protocol = shift;
60
61 my $container = {};
62
63 # $hContext is required therefore we check for its value
64 return (undef) unless (defined $hContext->{hContext});
65
66 # Keep a handle on the given PCSC context
67 $container->{hContext} = $hContext;
68
69 # if the user wants to initiate the connection we call _Connect()
70 # for him and return the appropriate error code if required
71 if (defined ($reader_name))
72 {
73 # Apply default values when required
74 $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE unless (defined($share_mode));
75 $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1 unless (defined ($preferred_protocol));
76
77 ($container->{hCard}, $container->{dwProtocol}) = Chipcard::PCSC::_Connect ($hContext->{hContext}, $reader_name, $share_mode, $preferred_protocol);
78 return (undef) unless (defined $container->{hCard});
79 }
80
81 # At this point, either the connection was successful or the user
82 # did not ask for a connection... in any case, we just return our
83 # blessed reference
84 return bless $container, $class;
85 } # new
86
87 # Usage:
88 # Connect ($reader_name, $share_mode, $preferred_protocol)
89 #
90 # default values:
91 # $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
92 # $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1
93 sub Connect
94 {
95 my $self = shift;
96 confess ("wrong type") unless ref $self;
97
98 # The object may be connected already so we check for that case
99 if (defined ($self->{hCard}))
100 {
101 $Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_ALREADY_CONNECTED;
102 return (undef);
103 }
104
105 # otherwise, we just pop the other parameters
106 my $reader_name = shift;
107 my $share_mode = shift;
108 my $preferred_protocol = shift;
109
110 # $reader_name is required so we check for its value
111 return (undef) unless (defined($reader_name));
112
113 # Apply default values when required
114 $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE unless (defined($share_mode));
115 $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1 unless (defined ($preferred_protocol));
116
117 ($self->{hCard}, $self->{dwProtocol}) = Chipcard::PCSC::_Connect ($self->{hContext}{hContext}, $reader_name, $share_mode, $preferred_protocol);
118
119 # We return the current protocole being used or undef if an error
120 # occured in this case, $self->{dwProtocol} should be undef
121 return $self->{dwProtocol};
122 } # Connect
123
124 # Usage:
125 # Reconnect ($share_mode, $preferred_protocol, $initialization)
126 #
127 # default values:
128 # $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
129 # $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1
130 # $initialization = $Chipcard::PCSC::SCARD_LEAVE_CARD
131 sub Reconnect
132 {
133 my $self = shift;
134 confess ("wrong type") unless ref $self;
135
136 # check whever we know the card handle or not
137
138 unless (defined ($self->{hCard}))
139 {
140 $Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_NOT_CONNECTED;
141 return (undef);
142 }
143
144 my $share_mode = shift;
145 my $preferred_protocol = shift;
146 my $initialization = shift;
147
148 # Apply default values when required
149 $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE unless (defined($share_mode));
150 $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1 unless (defined ($preferred_protocol));
151 $initialization = $Chipcard::PCSC::SCARD_LEAVE_CARD unless (defined($initialization));
152
153 $self->{dwProtocol} = Chipcard::PCSC::_Reconnect ($self->{hCard}, $share_mode, $preferred_protocol, $initialization);
154 return ($self->{dwProtocol});
155 } # Reconnect
156
157 sub Disconnect
158 {
159 my $self = shift;
160 confess ("wrong type") unless ref $self;
161
162 # check whever we know the card handle or not
163 unless (defined ($self->{hCard}))
164 {
165 $Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_NOT_CONNECTED;
166 return (undef);
167 }
168
169 my $disposition = shift;
170
171 # Apply default values when required
172 $disposition = $Chipcard::PCSC::SCARD_LEAVE_CARD unless (defined ($disposition));
173
174 my $return_val = Chipcard::PCSC::_Disconnect ($self->{hCard}, $disposition);
175 $self->{hCard} = (undef) if ($return_val);
176
177 return $return_val;
178 } # Disconnect
179
180 sub Status
181 {
182 my $self = shift;
183 confess ("wrong type") unless ref $self;
184
185 return Chipcard::PCSC::_Status ($self->{hCard});
186 } # Status
187
188 sub Transmit
189 {
190 my $self = shift;
191 confess ("wrong type") unless ref $self;
192
193 my $send_data = shift;
194 my $trash;
195 my $ret;
196
197 confess ("not connected") unless defined $self->{hCard};
198 return (undef) unless defined $self->{dwProtocol};
199
200 # the APDU is at least 4 bytes (CLA, INS, P1, P2)
201 warn ("Transmit: short APDU ($#$send_data bytes sent)") unless $#$send_data >= 3;
202 #TODO:
203 # The following warning was supposed to be helpful but it is real
204 # pain in the ass... I therefore decided to remove it. I will leave
205 # this code for a little time just to make sure it really is useless
206 # then I plan to remove it...
207 # warn ("Transmit: APDU length does not match P3 (" . ($#$send_data-4) . " instead of $send_data->[4])") unless $send_data->[4] == $#$send_data-4;
208
209 ($trash, $ret) = Chipcard::PCSC::_Transmit ($self->{hCard}, $self->{dwProtocol}, $send_data);
210
211 return $ret if (defined($ret));
212
213 # else error
214 return (undef)
215 } # Transmit
216
217 sub Control
218 {
219 my $self = shift;
220 confess ("wrong type") unless ref $self;
221
222 my $controlcode = shift;
223 my $send_data = shift;
224
225 confess ("not connected") unless defined $self->{hCard};
226
227 return Chipcard::PCSC::_Control ($self->{hCard}, $controlcode, $send_data);
228 } # Control
229
230 sub BeginTransaction
231 {
232 my $self = shift;
233 confess ("wrong type") unless ref $self;
234
235 unless (defined ($self->{hCard}))
236 {
237 $Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_NOT_CONNECTED;
238 return (0);
239 }
240
241 return Chipcard::PCSC::_BeginTransaction ($self->{hCard});
242 } # BeginTransaction
243
244 sub EndTransaction
245 {
246 my $self = shift;
247 confess ("wrong type") unless ref $self;
248
249 unless (defined ($self->{hCard})) {
250 $Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_NOT_CONNECTED;
251 return (0);
252 }
253
254 my $disposition = shift;
255
256 # print "we got dispo = $disposition\n";
257 # Apply default values when required
258 $disposition = $Chipcard::PCSC::SCARD_LEAVE_CARD unless (defined ($disposition));
259
260 return Chipcard::PCSC::_EndTransaction ($self->{hCard}, $disposition);
261 } # EndTransaction
262
263 sub DESTROY
264 {
265 my $self = shift;
266 confess ("wrong type") unless ref $self;
267
268 $self->Disconnect ();
269 if (defined ($self->{hCard}))
270 {
271 warn ("PCSCCard object $self deleted but still connected");
272 }
273 } # DESTROY
274
275 # Usage:
276 # $text = ISO7816Error($sw)
277 #
278 # return the text version of the ISO 7816-4 error given in $sw
279 sub ISO7816Error($)
280 {
281 my $sw = shift;
282
283 # default error message
284 my $text = "Error not defined by ISO 7816";
285
286 return "wrong SW size for: $sw" unless (length($sw) == 5);
287
288 # split the two error bytes
289 my ($sw1, $sw2) = split / /, $sw;
290
291 $text = "Normal processing." if ($sw =~ m/90 00/);
292 $text = "0x$sw2 bytes of response still available." if ($sw1 =~ m/61/);
293 if ($sw1 =~ m/62/)
294 {
295 $text = "State of non-volatile memory unchanged. ";
296 $text .= "No information given." if ($sw2 =~ m/00/);
297 $text .= "Part of returned data my be corrupted." if ($sw2 =~ m/81/);
298 $text .= "End of file/record reached before reading Le bytes." if ($sw2 =~ m/82/);
299 $text .= "Selected file invalidated." if ($sw2 =~ m/83/);
300 $text .= "FCI not formated according to 5.1.5." if ($sw2 =~ m/84/);
301 }
302
303 if ($sw1 =~ m/63/)
304 {
305 $text = "State of non-volatile memory changed. ";
306 $text .= "No information given." if ($sw2 =~ m/00/);
307 $text .= "File filled up by the last write." if ($sw2 =~ m/81/);
308 $text .= "Counter: 0x" . substr($sw2, 1, 1) if ($sw2 =~ m/^C/);
309 }
310
311 $text = "State of non-volatile memory unchanged." if ($sw =~ m/64 00/);
312
313 if ($sw1 =~ m/65/)
314 {
315 $text = "State of non-volatile memory changed. ";
316 $text .= "Memory failure." if ($sw2 =~ m/81/);
317 }
318
319 $text = "Reserved for security-related issues." if ($sw1 =~ m/66/);
320 $text = "Wrong length." if ($sw =~ m/67 00/);
321
322 if ($sw1 =~ m/68/)
323 {
324 $text = "Functions in CLA not supported. ";
325 $text .= "Logical channel not supported." if ($sw2 =~ m/81/);
326 $text .= "Secure messaging not supported." if ($sw2 =~ m/82/);
327 }
328
329 if ($sw1 =~ m/69/)
330 {
331 $text = "Command not allowed. ";
332 $text .= "Command incompatible with file structure." if ($sw2 =~ m/81/);
333 $text .= "Security status not satisfied." if ($sw2 =~ m/82/);
334 $text .= "Authentication method blocked." if ($sw2 =~ m/83/);
335 $text .= "Referenced data invalidated." if ($sw2 =~ m/84/);
336 $text .= "Conditions of use not satisfied." if ($sw2 =~ m/85/);
337 $text .= "Command not allowed (no current EF)." if ($sw2 =~ m/86/);
338 $text .= "Expected SM data objects missing." if ($sw2 =~ m/87/);
339 $text .= "SM data objects incorrect." if ($sw2 =~ m/88/);
340 }
341
342 if ($sw1 =~ m/6A/)
343 {
344 $text = "Wrong parameter(s) P1-P2. ";
345 $text .= "Incorrect parameters in the data field." if ($sw2 =~ m/80/);
346 $text .= "Function not supported." if ($sw2 =~ m/81/);
347 $text .= "File not found." if ($sw2 =~ m/82/);
348 $text .= "Record not found." if ($sw2 =~ m/83/);
349 $text .= "Not enough memory space in the file." if ($sw2 =~ m/84/);
350 $text .= "Lc inconsistent with TLV structure." if ($sw2 =~ m/85/);
351 $text .= "Incorrect parameters P1-P2." if ($sw2 =~ m/86/);
352 $text .= "Lc inconsistent with P1-P2." if ($sw2 =~ m/87/);
353 $text .= "Referenced data not found." if ($sw2 =~ m/88/);
354 }
355
356 $text = "Wrong parameter(s) P1-P2." if ($sw =~ m/6B 00/);
357 $text = "Wrong length Le: should be 0x$sw2" if ($sw1 =~ m/6C/);
358 $text = "Instruction code not supported or invalid." if ($sw =~ m/6D 00/);
359 $text = "Class not supported." if ($sw =~ m/6E 00/);
360 $text = "No precise diagnosis." if ($sw =~ m/6F 00/);
361
362 return $text;
363 } # ISO7816Error
364
365 $Chipcard::PCSC::Card::Error = "";
366
367 # Usage:
368 # ($sw, $res) = TransmitWithCheck($apdu, $sw_expected);
369 # die "Error: $Chipcard::PCSC::Card::Error\n" unless defined $sw;
370 #
371 # With debug
372 # ($sw, $res) = TransmitWithCheck($apdu, $sw_expected, 1);
373 # die "Error: $Chipcard::PCSC::Card::Error\n" unless defined $sw;
374 #
375 # Example:
376 # ($sw, $res) = TransmitWithCheck("00 A4 01 00 02 01 00", "90 [10]0");
377 sub TransmitWithCheck
378 {
379 my $self = shift;
380 confess ("wrong type") unless ref $self;
381
382 my $command = shift;
383 my $sw_expected = shift;
384 my $debug = shift;
385
386 my ($send, $recv, $data, $sw);
387
388 # add needed spaces
389 $command =~ s/(..)/$1 /g if ($command !~ m/ /);
390 print "=> $command\n" if (defined $debug);
391
392 # Convert $command in a reference to an array
393 $send = Chipcard::PCSC::ascii_to_array($command);
394
395 # send the APDU
396 $recv = $self -> Transmit($send);
397 if (! defined $recv)
398 {
399 $Chipcard::PCSC::Card::Error = "Can't transmit data: $Chipcard::PCSC::errno";
400 return undef;
401 }
402
403 $data = Chipcard::PCSC::array_to_ascii($recv);
404 print "<= $data\n" if defined $debug;
405
406 # Status word
407 $sw = substr $data, length ($data) -5, 5, "";
408
409 print "SW: $sw (" . &Chipcard::PCSC::Card::ISO7816Error($sw) . ")\n" if defined $debug;
410
411 if ($sw !~ m/$sw_expected/)
412 {
413 #$sw = PCSC::ISO7816_ErrorStr($sw);
414 $Chipcard::PCSC::Card::Error = "ERROR: expected $sw_expected and got $sw\n";
415 return undef;
416 }
417
418 # normal execution and no error
419 return ($sw, $data);
420 } # TransmitWithCheck
421
422 1;
423