pySim/transport add support for T=1 protocol and fix APDU/TPDU layer conflicts

ETSI TS 102 221, section 7.3 specifies that UICCs (and eUICCs) may support two
different transport protocols: T=0 or T=1 or both. The spec also says that the
terminal must support both protocols.

This patch adds the necessary functionality to support the T=1 protocol
alongside the T=0 protocol. However, this also means that we have to sharpen
the lines between APDUs and TPDUs.

As this patch also touches the low level interface to readers it was also
manually tested with a classic serial reader. Calypso and AT command readers
were not tested.

Change-Id: I8b56d7804a2b4c392f43f8540e0b6e70001a8970
Related: OS#6367
This commit is contained in:
Philipp Maier
2024-10-28 17:28:43 +01:00
parent f951c56449
commit 852eff54df
25 changed files with 606 additions and 144 deletions

View File

@@ -24,11 +24,11 @@ import serial
from osmocom.utils import h2b, b2h, Hexstr
from pySim.exceptions import NoCardError, ProtocolError
from pySim.transport import LinkBase
from pySim.transport import LinkBaseTpdu
from pySim.utils import ResTuple
class SerialSimLink(LinkBase):
class SerialSimLink(LinkBaseTpdu):
""" pySim: Transport Link for serial (RS232) based readers included with simcard"""
name = 'Serial'
@@ -187,13 +187,13 @@ class SerialSimLink(LinkBase):
def _rx_byte(self):
return self._sl.read()
def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple:
def send_tpdu(self, tpdu: Hexstr) -> ResTuple:
pdu = h2b(pdu)
data_len = pdu[4] # P3
tpdu = h2b(tpdu)
data_len = tpdu[4] # P3
# Send first CLASS,INS,P1,P2,P3
self._tx_string(pdu[0:5])
self._tx_string(tpdu[0:5])
# Wait ack which can be
# - INS: Command acked -> go ahead
@@ -201,7 +201,7 @@ class SerialSimLink(LinkBase):
# - SW1: The card can apparently proceed ...
while True:
b = self._rx_byte()
if ord(b) == pdu[1]:
if ord(b) == tpdu[1]:
break
if b != '\x60':
# Ok, it 'could' be SW1
@@ -214,12 +214,12 @@ class SerialSimLink(LinkBase):
raise ProtocolError()
# Send data (if any)
if len(pdu) > 5:
self._tx_string(pdu[5:])
if len(tpdu) > 5:
self._tx_string(tpdu[5:])
# Receive data (including SW !)
# length = [P3 - tx_data (=len(pdu)-len(hdr)) + 2 (SW1//2) ]
to_recv = data_len - len(pdu) + 5 + 2
# length = [P3 - tx_data (=len(tpdu)-len(hdr)) + 2 (SW1//2) ]
to_recv = data_len - len(tpdu) + 5 + 2
data = bytes(0)
while len(data) < to_recv: