From 24a7f168bd53e97305d3dc2dea7b0d4bb28c20ab Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 14 Mar 2024 15:26:00 +0100 Subject: [PATCH] pcsc: open reader/card in EXCLUSIVE mode by default There was a support request hinting that other applications concurrently accessed the SIM and were messing up the card state while pySim-shell was running. Let's avoid such situations by opening the card/reader in EXCLUSIVE mode by default. If somebody really has a special use case, they can now add the --pcsc-shared flag to restore the legacy behavior (SHARED mode). Change-Id: I90d887714b559a4604708d3c6dd23b5e05f40576 --- pySim/transport/pcsc.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pySim/transport/pcsc.py b/pySim/transport/pcsc.py index 47976ae9..9198816d 100644 --- a/pySim/transport/pcsc.py +++ b/pySim/transport/pcsc.py @@ -25,6 +25,7 @@ from smartcard.CardConnection import CardConnection from smartcard.CardRequest import CardRequest from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, CardConnectionException from smartcard.System import readers +from smartcard.ExclusiveConnectCardConnection import ExclusiveConnectCardConnection from pySim.exceptions import NoCardError, ProtocolError, ReaderError from pySim.transport import LinkBase @@ -56,6 +57,8 @@ class PcscSimLink(LinkBase): raise ReaderError('No matching reader found for regex %s' % opts.pcsc_regex) self._con = self._reader.createConnection() + if not opts.pcsc_shared: + self._con = ExclusiveConnectCardConnection(self._con) def __del__(self): try: @@ -119,6 +122,8 @@ access smart card readers, and is available on a variety of operating systems, s Windows, MacOS X and Linux. Most vendors of smart card readers provide drivers that offer a PC/SC interface, if not even a generic USB CCID driver is used. You can use a tool like ``pcsc_scan -r`` to obtain a list of readers available on your system. """) + pcsc_group.add_argument('--pcsc-shared', action='store_true', + help='Open PC/SC reaer in SHARED access (default: EXCLUSIVE)') dev_group = pcsc_group.add_mutually_exclusive_group() dev_group.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None, help='Number of PC/SC reader to use for SIM access')