fix: re-create PCSCCardConnection to avoid stale hcontext on pyscard >= 2.2 - #1
Open
calu777 wants to merge 1 commit into
Open
fix: re-create PCSCCardConnection to avoid stale hcontext on pyscard >= 2.2#1calu777 wants to merge 1 commit into
calu777 wants to merge 1 commit into
Conversation
…>= 2.2
In pyscard 2.2.x, PCSCCardRequest.waitforcard() releases its internal
SCARD context before returning, leaving card_service.connection.hcontext
as None. The subsequent SCardConnect() call then fails with:
TypeError: Expected a python long as SCARDCONTEXT.
This breaks peru-dnie on systems shipping recent pyscard, including
Debian 13 (python3-pyscard 2.2.2) with Python 3.13.
Fix: instead of returning card_service.connection directly, re-create
a fresh PCSCCardConnection from the reader. This establishes a new
SCARD context with a valid hcontext.
The PCSCCardConnection(reader) constructor API has been stable across
pyscard 2.0.x and 2.2.x, so this change is backward-compatible.
Tested on:
- Debian 13.4, Python 3.13, pyscard 2.2.2
- DNIe v2 (chip NXP JCOP3 SecID P60, applet RENIEC)
- Operations verified: extract signature, sign + openssl verify (Verified OK)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a
TypeError: Expected a python long as SCARDCONTEXTraised when calling any peru-dnie command (extract,sign) on systems shipping pyscard 2.2 or newer, such as Debian 13 with Python 3.13.Symptom
Root cause
Under pyscard 2.2.x,
CardRequest.waitforcard()(viaPCSCCardRequest) establishes its own SCARD context, uses it to wait for and identify the card, and releases that context before returning. Thecard_serviceit returns carries aconnectionwhosehcontextis nowNone.When peru-dnie subsequently calls
connection.connect(),PCSCCardConnection.connect()tries to callSCardConnect(self.hcontext, ...)withhcontext = None, which fails the type check in pyscard's C extension.This was confirmed by inspecting the connection returned by
get_dnie_connection():Fix
Instead of returning
card_service.connectiondirectly -whose context has been released- construct a freshPCSCCardConnectionfrom the reader thatwaitforcard()already identified. ThePCSCCardConnection.__init__callsSCardEstablishContext()and provides a validhcontext.The
PCSCCardConnection(reader)constructor signature has been stable across pyscard 2.0.x and 2.2.x, so this is backward-compatible.Testing
Verified on:
python3-pyscard)Operations tested:
Notes
requirements.txtconstraint) and 2.2.x.pyscard~=2.0.7inrequirements.txttopyscard>=2.0.7,<3so users on Debian 13 / Ubuntu 24.04+ can install without dependency conflicts.