Chipcard-PCSC

 view release on metacpan or  search on metacpan

Card/Card.pm  view on Meta::CPAN


# 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);



( run in 1.180 second using v1.01-cache-2.11-cpan-39bf76dae61 )