aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pySim/transport/__init__.py69
-rw-r--r--pySim/transport/pcsc.py40
-rw-r--r--pySim/transport/serial.py41
3 files changed, 75 insertions, 75 deletions
diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py
index e69de29..c9715fc 100644
--- a/pySim/transport/__init__.py
+++ b/pySim/transport/__init__.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+""" pySim: PCSC reader transport link base
+"""
+
+#
+# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
+#
+# 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, see <http://www.gnu.org/licenses/>.
+#
+
+class LinkBase(object):
+
+ def reset_card(self):
+ """reset_card(): Resets the card (power down/up)
+ """
+ pass
+
+ def send_apdu_raw(self, pdu):
+ """send_apdu_raw(pdu): Sends an APDU with minimal processing
+
+ pdu : string of hexadecimal characters (ex. "A0A40000023F00")
+ return : tuple(data, sw), where
+ data : string (in hex) of returned data (ex. "074F4EFFFF")
+ sw : string (in hex) of status word (ex. "9000")
+ """
+ pass
+
+ def send_apdu(self, pdu):
+ """send_apdu(pdu): Sends an APDU and auto fetch response data
+
+ pdu : string of hexadecimal characters (ex. "A0A40000023F00")
+ return : tuple(data, sw), where
+ data : string (in hex) of returned data (ex. "074F4EFFFF")
+ sw : string (in hex) of status word (ex. "9000")
+ """
+ data, sw = self.send_apdu_raw(pdu)
+
+ if (sw is not None) and (sw[0:2] == '9f'):
+ pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
+ data, sw = self.send_apdu_raw(pdu_gr)
+
+ return data, sw
+
+ def send_apdu_checksw(self, pdu, sw="9000"):
+ """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW
+
+ pdu : string of hexadecimal characters (ex. "A0A40000023F00")
+ sw : string of 4 hexadecimal characters (ex. "9000")
+ return : tuple(data, sw), where
+ data : string (in hex) of returned data (ex. "074F4EFFFF")
+ sw : string (in hex) of status word (ex. "9000")
+ """
+ rv = self.send_apdu(pdu)
+ if sw.lower() != rv[1]:
+ raise RuntimeError("SW match failed ! Expected %s and got %s." % (sw.lower(), rv[1]))
+ return rv
diff --git a/pySim/transport/pcsc.py b/pySim/transport/pcsc.py
index c7be4a0..a242976 100644
--- a/pySim/transport/pcsc.py
+++ b/pySim/transport/pcsc.py
@@ -27,10 +27,11 @@ from smartcard.System import readers
from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
from pySim.exceptions import NoCardError
+from pySim.transport import LinkBase
from pySim.utils import h2i, i2h
-class PcscSimLink(object):
+class PcscSimLink(LinkBase):
def __init__(self, reader_number=0, observer=0):
r = readers();
@@ -57,13 +58,8 @@ class PcscSimLink(object):
return 1
def send_apdu_raw(self, pdu):
- """send_apdu_raw(pdu): Sends an APDU with minimal processing
+ """see LinkBase.send_apdu_raw"""
- pdu : string of hexadecimal characters (ex. "A0A40000023F00")
- return : tuple(data, sw), where
- data : string (in hex) of returned data (ex. "074F4EFFFF")
- sw : string (in hex) of status word (ex. "9000")
- """
apdu = h2i(pdu)
data, sw1, sw2 = self._con.transmit(apdu)
@@ -72,33 +68,3 @@ class PcscSimLink(object):
# Return value
return i2h(data), i2h(sw)
-
- def send_apdu(self, pdu):
- """send_apdu(pdu): Sends an APDU and auto fetch response data
-
- pdu : string of hexadecimal characters (ex. "A0A40000023F00")
- return : tuple(data, sw), where
- data : string (in hex) of returned data (ex. "074F4EFFFF")
- sw : string (in hex) of status word (ex. "9000")
- """
- data, sw = self.send_apdu_raw(pdu)
-
- if (sw is not None) and (sw[0:2] == '9f'):
- pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
- data, sw = self.send_apdu_raw(pdu_gr)
-
- return data, sw
-
- def send_apdu_checksw(self, pdu, sw="9000"):
- """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW
-
- pdu : string of hexadecimal characters (ex. "A0A40000023F00")
- sw : string of 4 hexadecimal characters (ex. "9000")
- return : tuple(data, sw), where
- data : string (in hex) of returned data (ex. "074F4EFFFF")
- sw : string (in hex) of status word (ex. "9000")
- """
- rv = self.send_apdu(pdu)
- if sw.lower() != rv[1]:
- raise RuntimeError("SW match failed ! Expected %s and got %s." % (sw.lower(), rv[1]))
- return rv
diff --git a/pySim/transport/serial.py b/pySim/transport/serial.py
index c61b4b5..d384146 100644
--- a/pySim/transport/serial.py
+++ b/pySim/transport/serial.py
@@ -27,10 +27,11 @@ import serial
import time
from pySim.exceptions import NoCardError, ProtocolError
+from pySim.transport import LinkBase
from pySim.utils import h2b, b2h
-class SerialSimLink(object):
+class SerialSimLink(LinkBase):
def __init__(self, device='/dev/ttyUSB0', baudrate=9600, rst='-rts', debug=False):
self._sl = serial.Serial(
@@ -125,13 +126,7 @@ class SerialSimLink(object):
return self._sl.read()
def send_apdu_raw(self, pdu):
- """send_apdu_raw(pdu): Sends an APDU with minimal processing
-
- pdu : string of hexadecimal characters (ex. "A0A40000023F00")
- return : tuple(data, sw), where
- data : string (in hex) of returned data (ex. "074F4EFFFF")
- sw : string (in hex) of status word (ex. "9000")
- """
+ """see LinkBase.send_apdu_raw"""
pdu = h2b(pdu)
data_len = ord(pdu[4]) # P3
@@ -182,33 +177,3 @@ class SerialSimLink(object):
# Return value
return b2h(data), b2h(sw)
-
- def send_apdu(self, pdu):
- """send_apdu(pdu): Sends an APDU and auto fetch response data
-
- pdu : string of hexadecimal characters (ex. "A0A40000023F00")
- return : tuple(data, sw), where
- data : string (in hex) of returned data (ex. "074F4EFFFF")
- sw : string (in hex) of status word (ex. "9000")
- """
- data, sw = self.send_apdu_raw(pdu)
-
- if (sw is not None) and (sw[0:2] == '9f'):
- pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
- data, sw = self.send_apdu_raw(pdu_gr)
-
- return data, sw
-
- def send_apdu_checksw(self, pdu, sw="9000"):
- """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW
-
- pdu : string of hexadecimal characters (ex. "A0A40000023F00")
- sw : string of 4 hexadecimal characters (ex. "9000")
- return : tuple(data, sw), where
- data : string (in hex) of returned data (ex. "074F4EFFFF")
- sw : string (in hex) of status word (ex. "9000")
- """
- rv = self.send_apdu(pdu)
- if sw.lower() != rv[1]:
- raise RuntimeError("SW match failed ! Expected %s and got %s." % (sw.lower(), rv[1]))
- return rv