/usr/lib/perl5/Chipcard/PCSC/Card.pm is in libpcsc-perl 1.4.13-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ###############################################################################
#
# Authors : Lionel VICTOR <lionel.victor@unforgettable.com>
# <lionel.victor@free.fr>
# Ludovic ROUSSEAU <ludovic.rousseau@free.fr>
# Compiler : gcc, Visual C++
# Target : Unix, Windows
#
# Description : Perl wrapper to the PCSC API
#
# Copyright (C) 2001 - Lionel VICTOR
# 2003-2008 - Ludovic ROUSSEAU
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
#
###############################################################################
# $Id: Card.pm,v 1.24 2008-03-26 17:10:08 rousseau Exp $
package Chipcard::PCSC::Card;
require Chipcard::PCSC;
use warnings;
use Carp;
use strict;
our $VERSION = '0.02';
# Usage:
# $hCard = new Chipcard::PCSC::Card ($hcontext);
# $hCard = new Chipcard::PCSC::Card ($hcontext, $reader_name, $share_mode,
# $preferred_protocol);
#
# the second version also connect to the supplied reader.
# when no connection has been required, use Connect()
#
# default values:
# $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
# $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1
sub new
{
my $class = shift;
my $hContext = shift;
my $reader_name = shift;
my $share_mode = shift;
my $preferred_protocol = shift;
my $container = {};
# $hContext is required therefore we check for its value
return (undef) unless (defined $hContext->{hContext});
# Keep a handle on the given PCSC context
$container->{hContext} = $hContext;
# if the user wants to initiate the connection we call _Connect()
# for him and return the appropriate error code if required
if (defined ($reader_name))
{
# Apply default values when required
$share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE unless (defined($share_mode));
$preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1 unless (defined ($preferred_protocol));
($container->{hCard}, $container->{dwProtocol}) = Chipcard::PCSC::_Connect ($hContext->{hContext}, $reader_name, $share_mode, $preferred_protocol);
return (undef) unless (defined $container->{hCard});
}
# At this point, either the connection was successful or the user
# did not ask for a connection... in any case, we just return our
# blessed reference
return bless $container, $class;
} # new
# Usage:
# Connect ($reader_name, $share_mode, $preferred_protocol)
#
# default values:
# $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
# $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1
sub Connect
{
my $self = shift;
confess ("wrong type") unless ref $self;
# The object may be connected already so we check for that case
if (defined ($self->{hCard}))
{
$Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_ALREADY_CONNECTED;
return (undef);
}
# otherwise, we just pop the other parameters
my $reader_name = shift;
my $share_mode = shift;
my $preferred_protocol = shift;
# $reader_name is required so we check for its value
return (undef) unless (defined($reader_name));
# Apply default values when required
$share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE unless (defined($share_mode));
$preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1 unless (defined ($preferred_protocol));
($self->{hCard}, $self->{dwProtocol}) = Chipcard::PCSC::_Connect ($self->{hContext}{hContext}, $reader_name, $share_mode, $preferred_protocol);
# We return the current protocole being used or undef if an error
# occured in this case, $self->{dwProtocol} should be undef
return $self->{dwProtocol};
} # Connect
# Usage:
# Reconnect ($share_mode, $preferred_protocol, $initialization)
#
# default values:
# $share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE
# $preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1
# $initialization = $Chipcard::PCSC::SCARD_LEAVE_CARD
sub Reconnect
{
my $self = shift;
confess ("wrong type") unless ref $self;
# check whever we know the card handle or not
unless (defined ($self->{hCard}))
{
$Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_NOT_CONNECTED;
return (undef);
}
my $share_mode = shift;
my $preferred_protocol = shift;
my $initialization = shift;
# Apply default values when required
$share_mode = $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE unless (defined($share_mode));
$preferred_protocol = $Chipcard::PCSC::SCARD_PROTOCOL_T0 | $Chipcard::PCSC::SCARD_PROTOCOL_T1 unless (defined ($preferred_protocol));
$initialization = $Chipcard::PCSC::SCARD_LEAVE_CARD unless (defined($initialization));
$self->{dwProtocol} = Chipcard::PCSC::_Reconnect ($self->{hCard}, $share_mode, $preferred_protocol, $initialization);
return ($self->{dwProtocol});
} # Reconnect
sub Disconnect
{
my $self = shift;
confess ("wrong type") unless ref $self;
# check whever we know the card handle or not
unless (defined ($self->{hCard}))
{
$Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_NOT_CONNECTED;
return (undef);
}
my $disposition = shift;
# Apply default values when required
$disposition = $Chipcard::PCSC::SCARD_LEAVE_CARD unless (defined ($disposition));
my $return_val = Chipcard::PCSC::_Disconnect ($self->{hCard}, $disposition);
$self->{hCard} = (undef) if ($return_val);
return $return_val;
} # Disconnect
sub Status
{
my $self = shift;
confess ("wrong type") unless ref $self;
return Chipcard::PCSC::_Status ($self->{hCard});
} # Status
sub Transmit
{
my $self = shift;
confess ("wrong type") unless ref $self;
my $send_data = shift;
my $trash;
my $ret;
confess ("not connected") unless defined $self->{hCard};
return (undef) unless defined $self->{dwProtocol};
# the APDU is at least 4 bytes (CLA, INS, P1, P2)
warn ("Transmit: short APDU ($#$send_data bytes sent)") unless $#$send_data >= 3;
#TODO:
# The following warning was supposed to be helpful but it is real
# pain in the ass... I therefore decided to remove it. I will leave
# this code for a little time just to make sure it really is useless
# then I plan to remove it...
# warn ("Transmit: APDU length does not match P3 (" . ($#$send_data-4) . " instead of $send_data->[4])") unless $send_data->[4] == $#$send_data-4;
($trash, $ret) = Chipcard::PCSC::_Transmit ($self->{hCard}, $self->{dwProtocol}, $send_data);
return $ret if (defined($ret));
# else error
return (undef)
} # Transmit
sub Control
{
my $self = shift;
confess ("wrong type") unless ref $self;
my $controlcode = shift;
my $send_data = shift;
confess ("not connected") unless defined $self->{hCard};
return Chipcard::PCSC::_Control ($self->{hCard}, $controlcode, $send_data);
} # Control
sub BeginTransaction
{
my $self = shift;
confess ("wrong type") unless ref $self;
unless (defined ($self->{hCard}))
{
$Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_NOT_CONNECTED;
return (0);
}
return Chipcard::PCSC::_BeginTransaction ($self->{hCard});
} # BeginTransaction
sub EndTransaction
{
my $self = shift;
confess ("wrong type") unless ref $self;
unless (defined ($self->{hCard})) {
$Chipcard::PCSC::errno = $Chipcard::PCSC::SCARD_P_NOT_CONNECTED;
return (0);
}
my $disposition = shift;
# print "we got dispo = $disposition\n";
# Apply default values when required
$disposition = $Chipcard::PCSC::SCARD_LEAVE_CARD unless (defined ($disposition));
return Chipcard::PCSC::_EndTransaction ($self->{hCard}, $disposition);
} # EndTransaction
sub DESTROY
{
my $self = shift;
confess ("wrong type") unless ref $self;
$self->Disconnect ();
if (defined ($self->{hCard}))
{
warn ("PCSCCard object $self deleted but still connected");
}
} # DESTROY
# Usage:
# $text = ISO7816Error($sw)
#
# return the text version of the ISO 7816-4 error given in $sw
sub ISO7816Error($)
{
my $sw = shift;
# default error message
my $text = "Error not defined by ISO 7816";
return "wrong SW size for: $sw" unless (length($sw) == 5);
# split the two error bytes
my ($sw1, $sw2) = split / /, $sw;
$text = "Normal processing." if ($sw =~ m/90 00/);
$text = "0x$sw2 bytes of response still available." if ($sw1 =~ m/61/);
if ($sw1 =~ m/62/)
{
$text = "State of non-volatile memory unchanged. ";
$text .= "No information given." if ($sw2 =~ m/00/);
$text .= "Part of returned data my be corrupted." if ($sw2 =~ m/81/);
$text .= "End of file/record reached before reading Le bytes." if ($sw2 =~ m/82/);
$text .= "Selected file invalidated." if ($sw2 =~ m/83/);
$text .= "FCI not formated according to 5.1.5." if ($sw2 =~ m/84/);
}
if ($sw1 =~ m/63/)
{
$text = "State of non-volatile memory changed. ";
$text .= "No information given." if ($sw2 =~ m/00/);
$text .= "File filled up by the last write." if ($sw2 =~ m/81/);
$text .= "Counter: 0x" . substr($sw2, 1, 1) if ($sw2 =~ m/^C/);
}
$text = "State of non-volatile memory unchanged." if ($sw =~ m/64 00/);
if ($sw1 =~ m/65/)
{
$text = "State of non-volatile memory changed. ";
$text .= "Memory failure." if ($sw2 =~ m/81/);
}
$text = "Reserved for security-related issues." if ($sw1 =~ m/66/);
$text = "Wrong length." if ($sw =~ m/67 00/);
if ($sw1 =~ m/68/)
{
$text = "Functions in CLA not supported. ";
$text .= "Logical channel not supported." if ($sw2 =~ m/81/);
$text .= "Secure messaging not supported." if ($sw2 =~ m/82/);
}
if ($sw1 =~ m/69/)
{
$text = "Command not allowed. ";
$text .= "Command incompatible with file structure." if ($sw2 =~ m/81/);
$text .= "Security status not satisfied." if ($sw2 =~ m/82/);
$text .= "Authentication method blocked." if ($sw2 =~ m/83/);
$text .= "Referenced data invalidated." if ($sw2 =~ m/84/);
$text .= "Conditions of use not satisfied." if ($sw2 =~ m/85/);
$text .= "Command not allowed (no current EF)." if ($sw2 =~ m/86/);
$text .= "Expected SM data objects missing." if ($sw2 =~ m/87/);
$text .= "SM data objects incorrect." if ($sw2 =~ m/88/);
}
if ($sw1 =~ m/6A/)
{
$text = "Wrong parameter(s) P1-P2. ";
$text .= "Incorrect parameters in the data field." if ($sw2 =~ m/80/);
$text .= "Function not supported." if ($sw2 =~ m/81/);
$text .= "File not found." if ($sw2 =~ m/82/);
$text .= "Record not found." if ($sw2 =~ m/83/);
$text .= "Not enough memory space in the file." if ($sw2 =~ m/84/);
$text .= "Lc inconsistent with TLV structure." if ($sw2 =~ m/85/);
$text .= "Incorrect parameters P1-P2." if ($sw2 =~ m/86/);
$text .= "Lc inconsistent with P1-P2." if ($sw2 =~ m/87/);
$text .= "Referenced data not found." if ($sw2 =~ m/88/);
}
$text = "Wrong parameter(s) P1-P2." if ($sw =~ m/6B 00/);
$text = "Wrong length Le: should be 0x$sw2" if ($sw1 =~ m/6C/);
$text = "Instruction code not supported or invalid." if ($sw =~ m/6D 00/);
$text = "Class not supported." if ($sw =~ m/6E 00/);
$text = "No precise diagnosis." if ($sw =~ m/6F 00/);
return $text;
} # ISO7816Error
$Chipcard::PCSC::Card::Error = "";
# Usage:
# ($sw, $res) = TransmitWithCheck($apdu, $sw_expected);
# die "Error: $Chipcard::PCSC::Card::Error\n" unless defined $sw;
#
# With debug
# ($sw, $res) = TransmitWithCheck($apdu, $sw_expected, 1);
# die "Error: $Chipcard::PCSC::Card::Error\n" unless defined $sw;
#
# Example:
# ($sw, $res) = TransmitWithCheck("00 A4 01 00 02 01 00", "90 [10]0");
sub TransmitWithCheck
{
my $self = shift;
confess ("wrong type") unless ref $self;
my $command = shift;
my $sw_expected = shift;
my $debug = shift;
my ($send, $recv, $data, $sw);
# add needed spaces
$command =~ s/(..)/$1 /g if ($command !~ m/ /);
print "=> $command\n" if (defined $debug);
# Convert $command in a reference to an array
$send = Chipcard::PCSC::ascii_to_array($command);
# send the APDU
$recv = $self -> Transmit($send);
if (! defined $recv)
{
$Chipcard::PCSC::Card::Error = "Can't transmit data: $Chipcard::PCSC::errno";
return undef;
}
$data = Chipcard::PCSC::array_to_ascii($recv);
print "<= $data\n" if defined $debug;
# Status word
$sw = substr $data, length ($data) -5, 5, "";
print "SW: $sw (" . &Chipcard::PCSC::Card::ISO7816Error($sw) . ")\n" if defined $debug;
if ($sw !~ m/$sw_expected/)
{
#$sw = PCSC::ISO7816_ErrorStr($sw);
$Chipcard::PCSC::Card::Error = "ERROR: expected $sw_expected and got $sw\n";
return undef;
}
# normal execution and no error
return ($sw, $data);
} # TransmitWithCheck
1;
|