diff --git a/pySim/transport/pcsc.py b/pySim/transport/pcsc.py index c92a3ba2..21d3ce4d 100644 --- a/pySim/transport/pcsc.py +++ b/pySim/transport/pcsc.py @@ -26,6 +26,7 @@ from smartcard.CardRequest import CardRequest from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, CardConnectionException from smartcard.System import readers from smartcard.ExclusiveConnectCardConnection import ExclusiveConnectCardConnection +from smartcard.ATR import ATR from osmocom.utils import h2i, i2h, Hexstr @@ -84,18 +85,21 @@ class PcscSimLink(LinkBaseTpdu): self.disconnect() # Make card connection and select a suitable communication protocol + # (Even though pyscard provides an automatic protocol selection, we will make an independent decision + # based on the ATR. There are two reasons for that: + # 1) In case a card supports T=0 and T=1, we perfer to use T=0. + # 2) The automatic protocol selection may be unreliabe on some platforms + # see also: https://osmocom.org/issues/6952) self._con.connect() - supported_protocols = self._con.getProtocol(); - self.disconnect() - if (supported_protocols & CardConnection.T0_protocol): - protocol = CardConnection.T0_protocol + atr = ATR(self._con.getATR()) + if atr.isT0Supported(): + self._con.setProtocol(CardConnection.T0_protocol) self.set_tpdu_format(0) - elif (supported_protocols & CardConnection.T1_protocol): - protocol = CardConnection.T1_protocol + elif atr.isT1Supported(): + self._con.setProtocol(CardConnection.T1_protocol) self.set_tpdu_format(1) else: raise ReaderError('Unsupported card protocol') - self._con.connect(protocol) except CardConnectionException as exc: raise ProtocolError() from exc except NoCardException as exc: