Debian Codegraph @

Card/Card.pod

raw
1 =head1 NAME
2
3 Chipcard::PCSC::Card - Smart card communication library
4
5 =head1 SYNOPSIS
6
7 $hCard = new Chipcard::PCSC::Card ($hContext, "GemPC430 0 0",
8 $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE);
9
10 $RecvData = $hCard->Transmit([0xBC,0xB0,0x09,0xC8, 2]);
11
12 $hCard->Disconnect($Chipcard::PCSC::SCARD_LEAVE_CARD);
13
14 $hCard->Status();
15
16 $hCard->BeginTransaction();
17
18 $hCard->EndTransaction();
19
20 $hCard->TransmitWithCheck($apdu, $sw_expected [, $debug]);
21
22 $hCard->Control($control_code, \@data);
23
24 ISO7816Error($sw);
25
26 =head1 DESCRIPTION
27
28 The C<Chipcard::PCSC::Card> module implements the
29 C<Chipcard::PCSC::Card> class. Objects from this class are used to
30 communicate with a given reader. They are constructed out of a reference
31 to a PCSC object that drives the reader.
32
33 For more information about PC/SC please read the F<pcscd(1)> man page.
34
35 A C<Chipcard::PCSC::Card> object uses the following property:
36
37 =over 4
38
39 =item *
40 B<$pcsccard_object-E<gt>{hContext}>
41
42 the reference to the underlying PCSC object
43
44 =item *
45 B<$pcsccard_object-E<gt>{hCard}>
46
47 the current PCSC connection handle
48
49 =item *
50 B<$pcsccard_object-E<gt>{dwProtocol}>
51
52 the protocol being used
53
54 =back
55
56 =head1 CONSTRUCTORS
57
58 The following methods construct a C<Chipcard::PCSC::Card> object:
59
60 =over 4
61
62 =item *
63 B<$hCard = new Chipcard::PCSC::Card ($hContext);>
64
65
66 Constructs a new C<Chipcard::PCSC::Card> object without connecting to
67 any reader.
68
69 C<$hContext> is mandatory and contains the reference to a valid PCSC
70 object.
71
72 =item *
73 B<$hCard = new Chipcard::PCSC::Card ($hContext, $reader_name, $share_mode, $preferred_protocol);>
74
75 Constructs a new Chipcard::PCSC::Card object and connect to the
76 specified reader.
77
78 =over 4
79
80 =item *
81 $hContext
82
83 is mandatory and contains the reference to a valid PCSC object.
84
85 =item *
86 $reader_name
87
88 is the name of the reader you want to connect to. It is of the form
89 "GemPC410 0 0".
90
91 Please note that the list of available readers can be obtained with a
92 call to C<$hContext-E<gt>ListReaders()>. (See the section named F<PCSC
93 METHODS> in the F<Chipcard::PCSC> man page for more information on
94 C<ListReaders>).
95
96 =item *
97 $share_mode
98
99 is the desired mode of connection to the reader. It can be
100 any of the following:
101
102 =over 4
103
104 =item *
105 $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
106
107 the application do not share the reader
108
109 =item *
110 $Chipcard::PCSC::SCARD_SHARE_SHARED
111
112 the application will allow others to share the reader.
113
114 =item *
115 $Chipcard::PCSC::SCARD_SHARE_DIRECT
116
117 (not used by PC/SC-lite)
118
119 =back
120
121 =item *
122 $preferred_protocol
123
124 is the protocol which should be used if possible. If the protocol is
125 not available, another protocol will be used and
126 C<$hCard-E<gt>{dwProtocol}> will be set accordingly. Both
127 C<$hCard-E<gt>{dwProtocol}> and C<$preferred_protocol> accept the
128 following values:
129
130 =over 4
131
132 =item *
133 $Chipcard::PCSC::SCARD_PROTOCOL_T0
134
135 the T=0 protocol
136
137 =item *
138 $Chipcard::PCSC::SCARD_PROTOCOL_T1
139
140 the T=1 protocol
141
142 =item *
143 $Chipcard::PCSC::SCARD_PROTOCOL_RAW
144
145 raw protocol
146
147 =back
148
149 =back
150
151 =item *
152 B<$hCard = new Chipcard::PCSC::Card ($hContext, $reader_name, $share_mode);>
153
154 This method is equivalent to:
155
156 $hCard = new Chipcard::PCSC::Card ($hContext, $reader_name,
157 $share_mode,
158 $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
159 $Chipcard::PCSC::SCARD_PROTOCOL_T1);
160
161 =item *
162 B<$hCard = new Chipcard::PCSC::Card ($hContext, $reader_name);>
163
164 This method is equivalent to:
165
166 $hCard = new Chipcard::PCSC::Card ($hContext, $reader_name,
167 $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE,
168 $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
169 $Chipcard::PCSC::SCARD_PROTOCOL_T1);
170
171 =back
172
173 =head1 CONSTRUCTION FAILURE
174
175 C<Chipcard::PCSC::Card> constructors return an C<undef> value when the
176 object can not be created. C<$Chipcard::PCSC::errno> can be used to get
177 more information about the error. See section F<ERROR HANDLING> in
178 F<Chipcard::PCSC> man page for more information.
179
180 =head1 Chipcard::PCSC::Card METHODS
181
182 Here is a list of all the methods that can be used with a C<Chipcard::PCSC::Card>
183 object.
184
185 =head2
186 $hCard-E<gt>Connect($reader_name, $share_mode, $preferred_protocol);
187
188 C<Connect()> can be used to connect to the reader and its smart card if
189 the connection has not been established yet. The default constructor can
190 establish the connection if given enough parameters.
191
192 The return value upon successful completion is the protocol used to
193 communicate with the smart card. It can be any of the following:
194
195 =over 4
196
197 =item *
198 $Chipcard::PCSC::SCARD_PROTOCOL_T0
199
200 the T=0 protocol
201
202 =item *
203 $Chipcard::PCSC::SCARD_PROTOCOL_T1
204
205 the T=1 protocol
206
207 =item *
208 $Chipcard::PCSC::SCARD_PROTOCOL_RAW
209
210 raw protocol
211
212 =back
213
214 =over 4
215
216 =item *
217 $reader_name
218
219 is mandatory. It contains the name of the reader you want to connect to.
220 It is of the form "GemPC410 0 0".
221
222 Please note that the list of available readers can be obtained with a
223 call to C<$hContext-E<gt>ListReaders()>. (See the section named F<PCSC
224 METHODS> in the F<Chipcard::PCSC> man page for more information on
225 C<ListReaders>).
226
227 =item *
228 $share_mode
229
230 is the desired mode of connection to the reader. It can be any of the
231 following:
232
233 =over 4
234
235 =item *
236 $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
237
238 the application do not share the reader
239
240 =item *
241 $Chipcard::PCSC::SCARD_SHARE_SHARED
242
243 the application will allow others to share the reader.
244
245 =item *
246 $Chipcard::PCSC::SCARD_SHARE_DIRECT
247
248 (not used by PCSClite)
249
250 =back
251
252 =item *
253 $preferred_protocol
254
255 is the protocol which should be used if possible. If the protocol is not
256 available, another protocol will be used and
257 C<$hCard-E<gt>{dwProtocol}> will be set accordingly.
258 C<$preferred_protocol> accept the following values:
259
260 =over 4
261
262 =item *
263 $Chipcard::PCSC::SCARD_PROTOCOL_T0
264
265 the T=0 protocol
266
267 =item *
268 $Chipcard::PCSC::SCARD_PROTOCOL_T1
269
270 the T=1 protocol
271
272 =item *
273 $Chipcard::PCSC::SCARD_PROTOCOL_RAW
274
275 raw protocol
276
277 =back
278
279 =back
280
281 =head2
282 $hCard-E<gt>Connect($reader_name, $share_mode);
283
284 This method is equivalent to:
285
286 $hCard->Connect($reader_name, $share_mode,
287 $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
288 $Chipcard::PCSC::SCARD_PROTOCOL_T1);
289
290 =head2
291 $hCard-E<gt>Connect($reader_name);
292
293 This method is equivalent to:
294
295 $hCard->Connect($reader_name, $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE,
296 $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
297 $Chipcard::PCSC::SCARD_PROTOCOL_T1);
298
299 =head2
300 $hCard-E<gt>Reconnect($share_mode, $preferred_protocol, $initialization);
301
302 C<Reconnect()> can be used to re-negotiate an already opened connection.
303 This implies that the C<Chipcard::PCSC::Card> object is connected and
304 has C<$hCard-E<gt>{hCard}> set accordingly.
305
306 Reconnecting to a smart card is used to change the share mode and the
307 current protocol.
308
309 The return value upon successful completion is the protocol choose to
310 communicate with the smart card. It can be any of the following:
311
312 =over 4
313
314 =item *
315 $Chipcard::PCSC::SCARD_PROTOCOL_T0
316
317 the T=0 protocol
318
319 =item *
320 $Chipcard::PCSC::SCARD_PROTOCOL_T1
321
322 the T=1 protocol
323
324 =item *
325 $Chipcard::PCSC::SCARD_PROTOCOL_RAW
326
327 raw protocol
328
329 =back
330
331 =over 4
332
333 =item *
334 $share_mode
335
336 is the desired mode of connection to the reader. It can be any of the
337 following:
338
339 =over 4
340
341 =item *
342 $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
343
344 the application do not share the reader
345
346 =item *
347 $Chipcard::PCSC::SCARD_SHARE_SHARED
348
349 the application will allow others to share the reader.
350
351 =item *
352 $Chipcard::PCSC::SCARD_SHARE_DIRECT
353
354 (not used by PCSClite)
355
356 =back
357
358 =item *
359 $preferred_protocol
360
361 is the protocol which should be used if possible. If the protocol is not
362 available, another protocol will be used and
363 C<$hCard-E<gt>{dwProtocol}> will be set accordingly.
364 C<$preferred_protocol> accept the following values:
365
366 =over 4
367
368 =item *
369 $Chipcard::PCSC::SCARD_PROTOCOL_T0
370
371 the T=0 protocol
372
373 =item *
374 $Chipcard::PCSC::SCARD_PROTOCOL_T1
375
376 the T=1 protocol
377
378 =item *
379 $Chipcard::PCSC::SCARD_PROTOCOL_RAW
380
381 raw protocol
382
383 =back
384
385 =item *
386 $initialization
387
388 is the action to take when reconnecting to the smart card. It can be any
389 of the following values:
390
391 =over 4
392
393 =item *
394 $Chipcard::PCSC::SCARD_LEAVE_CARD
395
396 do nothing on close
397
398 =item *
399 $Chipcard::PCSC::SCARD_RESET_CARD
400
401 reset on close
402
403 =item *
404 $Chipcard::PCSC::SCARD_UNPOWER_CARD
405
406 power down on close
407
408 =item *
409 $Chipcard::PCSC::SCARD_EJECT_CARD
410
411 eject on close
412
413 =back
414
415 =back
416
417 =head2
418 $hCard-E<gt>Reconnect($share_mode, $preferred_protocol);
419
420 This method is equivalent to:
421
422 $hCard->Reconnect($share_mode, $preferred_protocol,
423 $Chipcard::PCSC::SCARD_LEAVE_CARD);
424
425 =head2
426 $hCard-E<gt>Reconnect($share_mode);
427
428 This method is equivalent to:
429
430 $hCard->Reconnect($share_mode,
431 $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
432 $Chipcard::PCSC::SCARD_PROTOCOL_T1,
433 $Chipcard::PCSC::SCARD_LEAVE_CARD);
434
435 =head2
436 $hCard-E<gt>Reconnect();
437
438 This method is equivalent to:
439
440 $hCard->Reconnect($Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE,
441 $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
442 $Chipcard::PCSC::SCARD_PROTOCOL_T1,
443 $Chipcard::PCSC::SCARD_LEAVE_CARD);
444
445 =head2
446 $hCard-E<gt>Disconnect($initialization);
447
448 C<Disconnect()> closes the connection to the smart card reader. It
449 returns true upon successful completion or undef otherwise.
450 C<$hCard-E<gt>{hContext}> will be set to undef if the connection is
451 successfully closed.
452
453 =over 4
454
455 =item *
456 $initialization
457
458 is the action to take when reconnecting to the smart card. It can be any
459 of the following values:
460
461 =over 4
462
463 =item *
464 $Chipcard::PCSC::SCARD_LEAVE_CARD
465
466 do nothing on close
467
468 =item *
469 $Chipcard::PCSC::SCARD_RESET_CARD
470
471 reset on close
472
473 =item *
474 $Chipcard::PCSC::SCARD_UNPOWER_CARD
475
476 power down on close
477
478 =item *
479 $Chipcard::PCSC::SCARD_EJECT_CARD
480
481 eject on close
482
483 =back
484
485 =back
486
487 =head2
488 $hCard-E<gt>Disconnect();
489
490 This method is equivalent to:
491
492 $hCard->Disconnect($Chipcard::PCSC::SCARD_EJECT_CARD);
493
494 =head2
495 $hCard-E<gt>Status();
496
497 C<Status()> returns the current status of the connection to a smart card.
498 It is used to retrieve the ATR (Answer To Reset) value as well as the
499 protocol being used to communicate.
500
501 The return value is the C<undef> value if an error occurs. In such a
502 case, C<$!> should be set with a string describing the error. Upon
503 successful completion C<Status> returns an array as follows:
504 (C<$reader_name>, C<$reader_state>, C<$protocol>, C<\@atr>)
505
506 =over 4
507
508 =item *
509 $reader_name
510
511 is a string containing the name of the reader
512
513 =item *
514 $reader_state
515
516 is a scalar containing the current state of the reader.
517
518 It can be any combination of the following values:
519
520 =over 5
521
522 =item *
523 $Chipcard::PCSC::SCARD_UNKNOWN
524
525 unknown state
526
527 =item *
528 $Chipcard::PCSC::SCARD_ABSENT
529
530 card is absent
531
532 =item *
533 $Chipcard::PCSC::SCARD_PRESENT
534
535 card is present
536
537 =item *
538 $Chipcard::PCSC::SCARD_SWALLOWED
539
540 card not powered
541
542 =item *
543 $Chipcard::PCSC::SCARD_POWERED
544
545 card is powered
546
547 =item *
548 $Chipcard::PCSC::SCARD_NEGOTIABLE
549
550 ready for PTS
551
552 =item *
553 $Chipcard::PCSC::SCARD_SPECIFIC
554
555 PTS has been set
556
557 =back
558
559 =item *
560 $protocol
561
562 is the protocol being used. It can be any of the following values:
563
564 =over 5
565
566 =item *
567 $Chipcard::PCSC::SCARD_PROTOCOL_T0,
568
569 =item *
570 $Chipcard::PCSC::SCARD_PROTOCOL_T1,
571
572 =item *
573 $Chipcard::PCSC::SCARD_PROTOCOL_RAW
574
575 =back
576
577 =item *
578 \@atr
579
580 is a reference to an array containing the ATR. Each cell of the array
581 contains one byte of the ATR. This parameter is however optional as the
582 ATR may not be available under some circumstances. For instance when the
583 card is not inserted, no ATR can be returned and this parameter will be
584 C<undef>.
585
586 =back
587
588 =head2
589 $hCard-E<gt>Transmit(\@data);
590
591 C<Transmit()> is used to exchange data with the card.
592
593 It returns a reference to an anonymous array holding the answer to the
594 emitted data. In case of an error, the reference is the C<undef> value.
595
596 =over 4
597
598 =item *
599 \@data
600
601 is a reference to the data to be sent to the card.
602
603 =back
604
605 Here is a small sample of how to use C<transmit>:
606
607 $SendData = [0x00, 0xA4, 0x01, 0x00, 0x02, 0x01, 0x00];
608 $RecvData = $hCard->Transmit($SendData);
609
610 print " Recv = ";
611 foreach $tmpVal (@{$RecvData}) {
612 printf ("%02X ", $tmpVal);
613 } print "\n";
614
615 =head2
616 $hCard-E<gt>BeginTransaction();
617
618 C<BeginTransaction()> is used to temporarily get exclusive control over
619 the smart card.
620
621 It returns TRUE upon successful completion and FALSE otherwise.
622 C<$Chipcard::PCSC::errno> should be set accordingly in case of an error.
623 See section F<ERROR HANDLING> in F<Chipcard::PCSC> man page for more
624 information.
625
626 =head2
627 $hCard-E<gt>EndTransaction($disposition);
628
629 C<EndTransaction()> is used to end a transaction initiated with
630 C<BeginTransaction()>.
631
632 It returns C<TRUE> upon successful completion and FALSE otherwise.
633 C<$Chipcard::PCSC::errno> should be set accordingly in case of an error.
634 See section F<ERROR HANDLING> in F<Chipcard::PCSC> man page for more
635 information.
636
637 =over 4
638
639 =item *
640 $disposition
641
642 is the action to take when ending the transaction. It can be any of the
643 following values:
644
645 =over 4
646
647 =item *
648 $Chipcard::PCSC::SCARD_LEAVE_CARD
649
650 do nothing on close
651
652 =item *
653 $Chipcard::PCSC::SCARD_RESET_CARD
654
655 reset on close
656
657 =item *
658 $Chipcard::PCSC::SCARD_UNPOWER_CARD
659
660 power down on close
661
662 =item *
663 $Chipcard::PCSC::SCARD_EJECT_CARD
664
665 eject on close
666
667 =back
668
669 =back
670
671 =head2
672 $hCard-E<gt>EndTransaction();
673
674 This method is equivalent to:
675
676 $hCard->EndTransaction($Chipcard::PCSC::SCARD_LEAVE_CARD);
677
678 =head2
679 $hCard-E<gt>TransmitWithCheck($apdu, $sw_expected [, $debug]);
680
681 This method is a wrapper around $hCard-E<gt>Transmit(). The
682 $apdu parameter is an ASCII text like "00 A4 01 00 02 01 00",
683 $sw_expected is a Perl regular expression like "90 [01]0".
684
685 If the status word returned matches the expression $sw_expected the
686 method returns a list ($sw, $recv). $sw is the status word (like "90
687 00") of the command, $recv is the result of the command.
688
689 If the status word do not match the expression $sw_expected the method
690 returns undef and the variable $Chipcard::PCSC::Card::Error is set.
691
692 The $debug argument is optional. If present the method will print on
693 stdout the command sent and the response from the card.
694
695 Example:
696
697 ($sw, $RecvData) = $hCard->TransmitWithCheck($SendData, "6E 00", 1);
698 warn "TransmitWithCheck: $Chipcard::PCSC::Card::Error" unless defined $sw;
699
700 =head2
701 $hCard-E<gt>Control($control_code, \@data);
702
703 This method uses PC/SC SCardControl() to send data specific to the
704 reader driver. See your driver documentation to know what data to use.
705
706 Example:
707
708 $data = Chipcard::PCSC::ascii_to_array ("01 23 45");
709 $RecvData = $hCard->Control(0x42000001, $SendData);
710 die ("Can't Control data: $Chipcard::PCSC::errno") unless (defined ($RecvData));
711
712 =head2
713 ISO7816Error($sw);
714
715 This method returns the ASCII text corresponding to the status word $sw
716 according to ISO 7816-4 specifications.
717
718 Example:
719
720 $sw = "90 00";
721 print "$sw: " . &Chipcard::PCSC::Card::ISO7816Error($sw) . "\n";
722
723 =head1 SEE ALSO
724
725 F<pcscd> man page has useful information about PC/SC-lite.
726 F<Chipcard::PCSC> man page holds all the necessary information to create
727 the PCSC object which will be the basis of C<Chipcard::PCSC::Card>.
728
729 =head1 COPYRIGHT
730
731 (C) Lionel VICTOR, 2001, GNU GPL
732
733 (C) Ludovic ROUSSEAU, 2003-2008, GNU GPL
734
735 =head1 AUTHORS / ACKNOWLEDGEMENT
736
737 Lionel VICTOR <[email protected]>
738 <[email protected]>
739
740 Ludovic ROUSSEAU <[email protected]>
741
742 =cut
743