pythonbeid 0.3.0__tar.gz → 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. pythonbeid-1.0.0/CHANGELOG.md +101 -0
  2. pythonbeid-1.0.0/CONTRIBUTING.md +45 -0
  3. pythonbeid-1.0.0/MANIFEST.in +3 -0
  4. pythonbeid-1.0.0/PKG-INFO +293 -0
  5. pythonbeid-1.0.0/README.md +246 -0
  6. pythonbeid-1.0.0/SECURITY.md +21 -0
  7. pythonbeid-1.0.0/pyproject.toml +133 -0
  8. pythonbeid-1.0.0/pythonbeid/__init__.py +83 -0
  9. pythonbeid-1.0.0/pythonbeid/__main__.py +3 -0
  10. pythonbeid-1.0.0/pythonbeid/card_reader.py +846 -0
  11. pythonbeid-1.0.0/pythonbeid/cli.py +176 -0
  12. pythonbeid-1.0.0/pythonbeid/exceptions.py +73 -0
  13. pythonbeid-1.0.0/pythonbeid/integrity.py +455 -0
  14. pythonbeid-1.0.0/pythonbeid/models.py +593 -0
  15. pythonbeid-1.0.0/pythonbeid/parser.py +472 -0
  16. pythonbeid-1.0.0/pythonbeid/py.typed +0 -0
  17. pythonbeid-1.0.0/pythonbeid/trust/belgium_root_ca2.pem +22 -0
  18. pythonbeid-1.0.0/pythonbeid/trust/belgium_root_ca3.pem +32 -0
  19. pythonbeid-1.0.0/pythonbeid/trust/belgium_root_ca4.pem +32 -0
  20. pythonbeid-1.0.0/pythonbeid/trust/belgium_root_ca6.pem +21 -0
  21. pythonbeid-1.0.0/pythonbeid.egg-info/PKG-INFO +293 -0
  22. pythonbeid-1.0.0/pythonbeid.egg-info/SOURCES.txt +36 -0
  23. pythonbeid-1.0.0/pythonbeid.egg-info/entry_points.txt +2 -0
  24. pythonbeid-1.0.0/pythonbeid.egg-info/requires.txt +15 -0
  25. pythonbeid-1.0.0/tests/__init__.py +0 -0
  26. pythonbeid-1.0.0/tests/builders.py +249 -0
  27. pythonbeid-1.0.0/tests/conftest.py +108 -0
  28. pythonbeid-1.0.0/tests/fake_card.py +227 -0
  29. pythonbeid-1.0.0/tests/test_card_reader.py +1012 -0
  30. pythonbeid-1.0.0/tests/test_cli.py +194 -0
  31. pythonbeid-1.0.0/tests/test_hardware.py +92 -0
  32. pythonbeid-1.0.0/tests/test_integrity.py +476 -0
  33. pythonbeid-1.0.0/tests/test_models.py +198 -0
  34. pythonbeid-1.0.0/tests/test_parser.py +484 -0
  35. pythonbeid-0.3.0/PKG-INFO +0 -104
  36. pythonbeid-0.3.0/README.md +0 -81
  37. pythonbeid-0.3.0/pyproject.toml +0 -29
  38. pythonbeid-0.3.0/pythonbeid/__init__.py +0 -13
  39. pythonbeid-0.3.0/pythonbeid/card_reader.py +0 -258
  40. pythonbeid-0.3.0/pythonbeid/exceptions.py +0 -27
  41. pythonbeid-0.3.0/pythonbeid/parser.py +0 -80
  42. pythonbeid-0.3.0/pythonbeid.egg-info/PKG-INFO +0 -104
  43. pythonbeid-0.3.0/pythonbeid.egg-info/SOURCES.txt +0 -14
  44. pythonbeid-0.3.0/pythonbeid.egg-info/requires.txt +0 -7
  45. pythonbeid-0.3.0/tests/test_card_reader.py +0 -258
  46. pythonbeid-0.3.0/tests/test_parser.py +0 -103
  47. {pythonbeid-0.3.0 → pythonbeid-1.0.0}/LICENSE +0 -0
  48. {pythonbeid-0.3.0 → pythonbeid-1.0.0}/pythonbeid.egg-info/dependency_links.txt +0 -0
  49. {pythonbeid-0.3.0 → pythonbeid-1.0.0}/pythonbeid.egg-info/top_level.txt +0 -0
  50. {pythonbeid-0.3.0 → pythonbeid-1.0.0}/setup.cfg +0 -0
@@ -0,0 +1,101 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
5
+ [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [1.0.0] - 2026-11-25
8
+
9
+ ### Added
10
+
11
+ - Typed API: `CardReader.read()` returns an immutable `EidData` with
12
+ `Identity`, `Address` and `CardInfo` dataclasses; `read_identity()`,
13
+ `read_address()`, `read_photo()`, `read_file()`, `read_certificate()`,
14
+ `read_card_info()`.
15
+ - Every field of the identity file defined by the specification v5.0
16
+ (tags `0x00` to `0x1F`), including noble condition, document type, special
17
+ status, photo and basic key digests and the fields of residence cards.
18
+ - `PartialDate` for incomplete birth dates, `Sex`, `DocumentType` and
19
+ `SpecialStatus` enums, `is_valid_national_number()`.
20
+ - Authenticity checks (`pip install "pythonbeid[verify]"`): photo digest,
21
+ identity and address signatures (RSA and ECDSA), RRN certificate chain to
22
+ the bundled Belgian roots CA2, CA3, CA4 and CA6 (end-entity signing
23
+ certificate only), consistency of the decoded data with the signed files,
24
+ basic key and chip challenge on applet 1.8 cards. `CardReader.verify()`,
25
+ `CardReader.authenticate_chip()`, `verify_eid()`, `verify_photo()`,
26
+ `basic_key_matches()`.
27
+ - `EidData.from_files()` to decode and verify raw files received from
28
+ elsewhere (e.g. on a server).
29
+ - Reader selection by index, name or substring; automatic selection of the
30
+ reader holding a Belgian eID card; `timeout` to wait for a card.
31
+ - Command line: `pythonbeid readers | info | read | verify` and
32
+ `python -m pythonbeid`.
33
+ - `py.typed` marker, `__version__`, `CardFile` enum of the card files.
34
+ - `BeidError` base exception, `PCSCServiceError`, `UnsupportedCardError`,
35
+ `CardDataError`; `APDUError.sw`.
36
+ - Card values (names, dates...) never appear in log or exception messages.
37
+
38
+ ### Fixed
39
+
40
+ - Birth dates written in Dutch or German: most months were silently read as
41
+ January, and German dates (`22.DEZ. 1997`) raised an error.
42
+ - Reading a file whose size is a multiple of the block size raised
43
+ `APDUError` (`6B00` at end of file).
44
+ - `61xx` answers were accepted without GET RESPONSE, silently returning
45
+ truncated data.
46
+ - TLV parsing ignored tags: fields were taken by position, which breaks when
47
+ a tag is absent or when the version tag `0x00` is present. Lengths of 255
48
+ bytes and more are now decoded as specified.
49
+ - Values kept their padding spaces and zero bytes.
50
+ - The field read as `suffix` is the first letter of the third given name.
51
+ - pyscard exceptions leaked out of the library; they are now wrapped.
52
+ - Closing the connection powered the card down for other applications.
53
+ - Negative reader indexes silently selected the last reader.
54
+
55
+ ### Changed
56
+
57
+ - Commands run inside PC/SC transactions; the connection recovers when
58
+ another application resets the card.
59
+ - Files are read in 252-byte blocks, like the official middleware.
60
+ - Requires Python 3.11 or later and pyscard 2.0.8 or later.
61
+ - Unknown month names raise `CardDataError` instead of defaulting to January.
62
+
63
+ ### Deprecated
64
+
65
+ - `CardReader.read_informations()` (use `read()`), `reader_index` (pass the
66
+ index as `reader`), `parse_tlv()` (use `decode_tlv()`),
67
+ `parse_french_date()` (use `parse_birth_date()`). They keep their 0.x
68
+ output and will be removed in 2.0.
69
+
70
+ ## [0.3.0] - 2026-06-10
71
+
72
+ ### Added
73
+
74
+ - `list_readers()` public function.
75
+ - Publication to PyPI with Trusted Publishing on version tags.
76
+
77
+ ## [0.2.0] - 2026-03-03
78
+
79
+ ### Changed
80
+
81
+ - Package restructured (`card_reader`, `parser`, `exceptions`), English
82
+ dictionary keys, context manager, dedicated exceptions, `pyproject.toml`,
83
+ test suite.
84
+
85
+ ## [0.1.1] - 2024-06-22
86
+
87
+ ### Fixed
88
+
89
+ - Packaging metadata.
90
+
91
+ ## [0.1.0] - 2024-06-22
92
+
93
+ ### Added
94
+
95
+ - First release on PyPI: identity, address and photo reading.
96
+
97
+ [1.0.0]: https://github.com/Lapin-Blanc/pythonbeid/compare/v0.3.0...v1.0.0
98
+ [0.3.0]: https://github.com/Lapin-Blanc/pythonbeid/releases/tag/v0.3.0
99
+ [0.2.0]: https://pypi.org/project/pythonbeid/0.2.0/
100
+ [0.1.1]: https://pypi.org/project/pythonbeid/0.1.1/
101
+ [0.1.0]: https://pypi.org/project/pythonbeid/0.1.0/
@@ -0,0 +1,45 @@
1
+ # Contributing
2
+
3
+ Contributions are welcome. Please open an issue first for significant changes.
4
+
5
+ ## Setup
6
+
7
+ ```bash
8
+ python -m venv .venv
9
+ . .venv/bin/activate # .venv\Scripts\activate on Windows
10
+ pip install -e ".[dev]"
11
+ pre-commit install
12
+ ```
13
+
14
+ On Linux, install `pcscd` and `libpcsclite-dev` first.
15
+
16
+ ## Checks
17
+
18
+ ```bash
19
+ pytest # simulated card, coverage must stay at or above 95 %
20
+ pytest --hardware # real card tests (they never print card data)
21
+ ruff check pythonbeid tests
22
+ black pythonbeid tests
23
+ mypy
24
+ ```
25
+
26
+ The CI runs the same checks on Linux, Windows and macOS for every supported
27
+ Python version.
28
+
29
+ ## Conventions
30
+
31
+ - Code, comments, docstrings (Google style) and commit messages in English.
32
+ - Type hints everywhere; `mypy --strict` must pass.
33
+ - [Conventional Commits](https://www.conventionalcommits.org/): `feat:`,
34
+ `fix:`, `docs:`, `refactor:`, `test:`, `chore:`, `ci:`.
35
+ - Work on a branch and open a pull request against `master`.
36
+ - **Never commit data read from a real card.** Tests use the synthetic
37
+ builders in `tests/builders.py` and the simulated card in
38
+ `tests/fake_card.py`.
39
+
40
+ ## Releasing
41
+
42
+ 1. Update the version in `pyproject.toml` and add a section to `CHANGELOG.md`.
43
+ 2. Merge to `master`, then push a `vX.Y.Z` tag.
44
+ 3. The *Publish to PyPI* workflow runs the CI, publishes with Trusted
45
+ Publishing and creates the GitHub release from the changelog.
@@ -0,0 +1,3 @@
1
+ graft tests
2
+ include CHANGELOG.md CONTRIBUTING.md SECURITY.md
3
+ global-exclude *.py[cod] __pycache__
@@ -0,0 +1,293 @@
1
+ Metadata-Version: 2.4
2
+ Name: pythonbeid
3
+ Version: 1.0.0
4
+ Summary: Read and verify Belgian eID cards (identity, address, photo) through PC/SC
5
+ Author-email: Fabien Toune <fabien.toune@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Lapin-Blanc/pythonbeid
8
+ Project-URL: Repository, https://github.com/Lapin-Blanc/pythonbeid
9
+ Project-URL: Issues, https://github.com/Lapin-Blanc/pythonbeid/issues
10
+ Project-URL: Changelog, https://github.com/Lapin-Blanc/pythonbeid/blob/master/CHANGELOG.md
11
+ Keywords: belgium,eid,beid,belpic,smartcard,pcsc,identity card
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Natural Language :: English
15
+ Classifier: Operating System :: MacOS
16
+ Classifier: Operating System :: Microsoft :: Windows
17
+ Classifier: Operating System :: POSIX :: Linux
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python :: 3.14
24
+ Classifier: Programming Language :: Python :: 3.15
25
+ Classifier: Topic :: Security
26
+ Classifier: Topic :: Security :: Cryptography
27
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
28
+ Classifier: Topic :: System :: Hardware
29
+ Classifier: Typing :: Typed
30
+ Requires-Python: >=3.11
31
+ Description-Content-Type: text/markdown
32
+ License-File: LICENSE
33
+ Requires-Dist: pyscard>=2.0.8
34
+ Provides-Extra: verify
35
+ Requires-Dist: cryptography>=42; extra == "verify"
36
+ Provides-Extra: dev
37
+ Requires-Dist: black>=25; extra == "dev"
38
+ Requires-Dist: build>=1.2; extra == "dev"
39
+ Requires-Dist: cryptography>=42; extra == "dev"
40
+ Requires-Dist: mypy>=1.13; extra == "dev"
41
+ Requires-Dist: pre-commit>=4; extra == "dev"
42
+ Requires-Dist: pytest>=8; extra == "dev"
43
+ Requires-Dist: pytest-cov>=6; extra == "dev"
44
+ Requires-Dist: ruff>=0.8; extra == "dev"
45
+ Requires-Dist: twine>=6; extra == "dev"
46
+ Dynamic: license-file
47
+
48
+ # pythonbeid
49
+
50
+ [![PyPI](https://img.shields.io/pypi/v/pythonbeid)](https://pypi.org/project/pythonbeid/)
51
+ [![Python](https://img.shields.io/pypi/pyversions/pythonbeid)](https://pypi.org/project/pythonbeid/)
52
+ [![CI](https://github.com/Lapin-Blanc/pythonbeid/actions/workflows/ci.yml/badge.svg)](https://github.com/Lapin-Blanc/pythonbeid/actions/workflows/ci.yml)
53
+ [![License: MIT](https://img.shields.io/pypi/l/pythonbeid)](LICENSE)
54
+
55
+ Read and verify **Belgian electronic identity cards** (eID, Kids-ID, residence
56
+ cards for foreign nationals) from Python, with any PC/SC smart card reader.
57
+
58
+ ```python
59
+ from pythonbeid import CardReader
60
+
61
+ with CardReader() as reader:
62
+ eid = reader.read()
63
+ print(eid.identity.last_name, eid.identity.birth_date)
64
+ print(eid.address.street, eid.address.postal_code, eid.address.municipality)
65
+ ```
66
+
67
+ ## Features
68
+
69
+ - **Complete card content**: every field of the identity and address files
70
+ (applet 1.7 and 1.8 layouts), the photo, chip information and certificates.
71
+ - **Correct in every language**: birth dates written in French, Dutch or
72
+ German, incomplete birth dates, `M`/`F`/`V`/`W` sex letters, document types
73
+ and special statuses.
74
+ - **Authenticity checks**: photo digest, National Register signatures of the
75
+ identity and address files, certificate chain up to the Belgian root CAs and,
76
+ on applet 1.8 cards, the chip challenge that detects cloned data.
77
+ - **Robust transport**: follows the official middleware (block size, `6Cxx`,
78
+ `61xx`, end of file), runs inside PC/SC transactions so other applications
79
+ cannot interfere, recovers when another application resets the card, and
80
+ leaves the card powered for them.
81
+ - **Typed API**: immutable dataclasses, enums and `datetime.date`, shipped
82
+ with `py.typed`; strict mypy clean.
83
+ - **Command line**: `pythonbeid read`, `pythonbeid verify`...
84
+ - No PIN is ever sent: only public card content is read.
85
+
86
+ ## Installation
87
+
88
+ ```bash
89
+ pip install pythonbeid # reading
90
+ pip install "pythonbeid[verify]" # reading + signature verification
91
+ ```
92
+
93
+ Requirements: Python 3.11 or later and a PC/SC reader.
94
+
95
+ - **Windows, macOS**: nothing else to install.
96
+ - **Linux**: the PC/SC daemon must run, and [pyscard](https://pypi.org/project/pyscard/)
97
+ is compiled at install time:
98
+
99
+ ```bash
100
+ sudo apt install pcscd libpcsclite-dev # Debian, Ubuntu, Mint, MX Linux...
101
+ ```
102
+
103
+ ## Reading a card
104
+
105
+ `CardReader()` picks the first reader that holds a Belgian eID card. You can
106
+ also choose the reader and wait for the card to be inserted:
107
+
108
+ ```python
109
+ from pythonbeid import CardReader, list_readers
110
+
111
+ print(list_readers()) # ['ACS ACR38U 0', ...]
112
+
113
+ with CardReader("ACR38U", timeout=30) as reader: # index, name or part of it
114
+ identity = reader.read_identity()
115
+ address = reader.read_address()
116
+ photo = reader.read_photo() # JPEG bytes
117
+ print(reader.card_info.applet_version_string) # "1.7" or "1.8"
118
+ ```
119
+
120
+ `reader.read()` reads everything in a single transaction and returns an
121
+ `EidData` object (`card`, `identity`, `address`, `photo`, raw `files`). Pass
122
+ `photo=False` or `integrity=False` to skip the photo or the files used by the
123
+ verification. All models offer `to_dict()` for JSON serialisation.
124
+
125
+ Reading speed depends on the reader: `read()` takes about 9 s on an old
126
+ 9600-baud ACS ACR38U and proportionally less on faster readers. The photo
127
+ (3 KB) is the largest file; skip it when you do not need it.
128
+
129
+ ### Identity fields
130
+
131
+ | Field | Type | Notes |
132
+ |---|---|---|
133
+ | `card_number`, `chip_number` | `str` | chip number in hexadecimal |
134
+ | `validity_start`, `validity_end` | `date` | `is_valid_on(day)` helper |
135
+ | `issuing_municipality` | `str` | |
136
+ | `national_number` | `str` | `national_number_valid` checks the modulo-97 checksum |
137
+ | `last_name` | `str` | |
138
+ | `first_names` | `str` | the first two given names only |
139
+ | `third_given_name_initial` | `str` | `given_names` joins both |
140
+ | `nationality`, `birth_place` | `str` | in the language of the card |
141
+ | `birth_date` | `PartialDate \| None` | `.to_date()`; year or month may be unknown; `None` for an unknown format (raw value in `raw[0x0C]`) |
142
+ | `sex` | `Sex \| None` | `Sex.MALE` / `Sex.FEMALE` |
143
+ | `noble_condition` | `str` | |
144
+ | `document_type` | `DocumentType \| None` | `.description`, `.is_kids_card`, `.is_foreigner_card` |
145
+ | `special_status` | `SpecialStatus` | `.white_cane`, `.yellow_cane`, `.extended_minority` |
146
+ | `photo_hash`, `basic_key_hash` | `bytes` | used by the verification |
147
+ | foreign nationals | | `duplicate`, `special_organization`, `member_of_family`, `date_and_country_of_protection`, `work_permit`, `employer_vat_1/2`, `regional_file_number`, `brexit_mention_1/2`, `card_a_mention_1/2`, `eu_registration_date` |
148
+ | `raw` | `Mapping[int, bytes]` | every TLV record, for future tags |
149
+
150
+ The address has `street` (street and house number), `postal_code` and
151
+ `municipality`.
152
+
153
+ ## Verifying authenticity
154
+
155
+ Requires the `verify` extra.
156
+
157
+ ```python
158
+ with CardReader() as reader:
159
+ report = reader.verify()
160
+ print(report.is_valid)
161
+ for error in report.errors:
162
+ print(error)
163
+ ```
164
+
165
+ | Check | Meaning |
166
+ |---|---|
167
+ | `photo_hash_valid` | the photo matches the digest in the identity file |
168
+ | `identity_signature_valid` | the identity file is signed by the National Register (RRN) |
169
+ | `address_signature_valid` | the address file is signed by the RRN (and bound to the identity signature) |
170
+ | `rrn_certificate_trusted` | the RRN certificate is an end-entity signing certificate issued by a bundled Belgian root CA |
171
+ | `rrn_certificate_period_valid` | the RRN certificate validity overlaps the card validity |
172
+ | `content_matches_files` | the decoded identity, address and photo are those of the signed files |
173
+ | `basic_key_valid` | applet 1.8: the chip public key is the one signed by the RRN |
174
+ | `chip_authenticated` | applet 1.8: the chip signed a random challenge with its private key |
175
+
176
+ A check that does not apply is `None`. The RRN certificate is not required to
177
+ be valid today: certificates of valid cards may have expired since, and cards
178
+ re-signed after issuance carry a newer one. Whether a card has a basic key is
179
+ decided by the signed identity file, never by the unsigned chip information,
180
+ so a clone cannot avoid the chip challenge by claiming an older applet.
181
+
182
+ Raw files can be checked offline, for instance on a server receiving them
183
+ from a client:
184
+
185
+ ```python
186
+ from pythonbeid import EidData, verify_eid
187
+
188
+ eid = EidData.from_files(files) # {CardFile: bytes}
189
+ report = verify_eid(eid) # everything is checked on the raw files
190
+ ```
191
+
192
+ The chip challenge needs the live card: `reader.authenticate_chip(identity)`
193
+ checks the chip against an identity file whose signature you verified.
194
+
195
+ Bundled trust anchors, downloaded from `certs.eid.belgium.be` and
196
+ `crt.eidpki.belgium.be` (SHA-256):
197
+
198
+ | Root | Fingerprint |
199
+ |---|---|
200
+ | Belgium Root CA2 | `9f9744463be13714754e1a3becf98c08cc205e4ab32028f4e2830c4a1b2775b8` |
201
+ | Belgium Root CA3 | `a8d14e945e3e5156bcae5e39737cf6a1b1f51028bbbf982f50ce5f4c05568b4d` |
202
+ | Belgium Root CA4 | `702dd5c1a093cf0a9d71fadd9bf9a7c5857d89fb73b716e867228b3c2beb968f` |
203
+ | Belgium Root CA6 | `9c872bc979a7c09a58d4a274c199e5cb16cfa9b9618d98bc9a9988e984b8495c` |
204
+
205
+ Pass `trusted_roots=[...]` (PEM or DER) to use your own.
206
+
207
+ ## Command line
208
+
209
+ ```console
210
+ $ pythonbeid readers
211
+ 0: ACS ACR38U 0
212
+ $ pythonbeid info # reader, ATR, applet version
213
+ $ pythonbeid read --json # identity and address
214
+ $ pythonbeid read --photo me.jpg
215
+ $ pythonbeid verify # exit status 3 when a check fails
216
+ ```
217
+
218
+ `python -m pythonbeid` works too. Options: `--reader`, `--timeout`, `--json`,
219
+ `-v` / `-vv` (APDU trace, without card data).
220
+
221
+ ## Errors
222
+
223
+ All exceptions derive from `pythonbeid.BeidError`:
224
+
225
+ | Exception | Raised when |
226
+ |---|---|
227
+ | `NoReaderError` | no reader, or none matches the requested one |
228
+ | `PCSCServiceError` | the PC/SC service is not running (subclass of `NoReaderError`) |
229
+ | `NoCardError` | no card in the reader, or the card was removed |
230
+ | `UnsupportedCardError` | the card is not a Belgian eID card, or lacks a feature |
231
+ | `CardCommunicationError` | transport failure |
232
+ | `APDUError` | the card refused a command (`.sw` holds the status word) |
233
+ | `CardDataError` | card content cannot be decoded (also a `ValueError`) |
234
+
235
+ ## Migrating from 0.x
236
+
237
+ The 0.x API still works and emits a `DeprecationWarning`:
238
+
239
+ | 0.x | 1.0 |
240
+ |---|---|
241
+ | `cr.read_informations(photo=True)` | `reader.read()` returning typed `EidData` |
242
+ | `CardReader(reader_index=1)` | `CardReader(1)` or `CardReader("name")` |
243
+ | `info["birth_date"]` (`datetime`) | `identity.birth_date` (`PartialDate`) |
244
+ | `info["suffix"]` | `identity.third_given_name_initial` |
245
+ | `info["address"]`, `info["city"]` | `address.street`, `address.municipality` |
246
+ | `info["photo"]` (base-64 `str`) | `eid.photo` (`bytes`) |
247
+ | `parse_tlv`, `parse_french_date` | `decode_tlv`, `parse_birth_date` |
248
+
249
+ Behaviour fixes that may be visible: birth dates written in Dutch or German
250
+ are now parsed correctly (0.x silently returned January for most months, and
251
+ failed on German cards); values are stripped of padding spaces; unknown month
252
+ names raise instead of defaulting to January.
253
+
254
+ ## Compatibility
255
+
256
+ - Applet 1.7 cards (RSA keys): validated on real cards.
257
+ - Applet 1.8 cards (elliptic-curve keys, current generation): implemented
258
+ after the official specification and middleware, and covered by simulated
259
+ tests. Reports from 1.8 card holders are welcome (see the issue template;
260
+ never share personal data).
261
+ - Python 3.11 to 3.15 on Windows, macOS and Linux.
262
+
263
+ ## Privacy
264
+
265
+ Card content is personal data (GDPR). pythonbeid never logs it: debug logs
266
+ only contain command headers, status words and lengths. Ask for consent and
267
+ keep only what you need.
268
+
269
+ ## Development
270
+
271
+ ```bash
272
+ git clone https://github.com/Lapin-Blanc/pythonbeid && cd pythonbeid
273
+ python -m venv .venv && . .venv/bin/activate # .venv\Scripts\activate on Windows
274
+ pip install -e ".[dev]" && pre-commit install
275
+ pytest # simulated card, no hardware needed
276
+ pytest --hardware # also runs the tests on a real card (no data is printed)
277
+ ```
278
+
279
+ See [CONTRIBUTING.md](CONTRIBUTING.md).
280
+
281
+ ## References
282
+
283
+ Official documentation, in the
284
+ [Fedict/eid-mw](https://github.com/Fedict/eid-mw/tree/master/doc/sdk/documentation)
285
+ repository:
286
+
287
+ - *ID and Address file contents*, applet 1.8, v5.0 (2023)
288
+ - *Belgian Electronic Identity Card content*, v5.4
289
+ - *Reference manual Belpic V1.8 eID card* and *V1.7*
290
+
291
+ ## License
292
+
293
+ MIT, see [LICENSE](LICENSE).
@@ -0,0 +1,246 @@
1
+ # pythonbeid
2
+
3
+ [![PyPI](https://img.shields.io/pypi/v/pythonbeid)](https://pypi.org/project/pythonbeid/)
4
+ [![Python](https://img.shields.io/pypi/pyversions/pythonbeid)](https://pypi.org/project/pythonbeid/)
5
+ [![CI](https://github.com/Lapin-Blanc/pythonbeid/actions/workflows/ci.yml/badge.svg)](https://github.com/Lapin-Blanc/pythonbeid/actions/workflows/ci.yml)
6
+ [![License: MIT](https://img.shields.io/pypi/l/pythonbeid)](LICENSE)
7
+
8
+ Read and verify **Belgian electronic identity cards** (eID, Kids-ID, residence
9
+ cards for foreign nationals) from Python, with any PC/SC smart card reader.
10
+
11
+ ```python
12
+ from pythonbeid import CardReader
13
+
14
+ with CardReader() as reader:
15
+ eid = reader.read()
16
+ print(eid.identity.last_name, eid.identity.birth_date)
17
+ print(eid.address.street, eid.address.postal_code, eid.address.municipality)
18
+ ```
19
+
20
+ ## Features
21
+
22
+ - **Complete card content**: every field of the identity and address files
23
+ (applet 1.7 and 1.8 layouts), the photo, chip information and certificates.
24
+ - **Correct in every language**: birth dates written in French, Dutch or
25
+ German, incomplete birth dates, `M`/`F`/`V`/`W` sex letters, document types
26
+ and special statuses.
27
+ - **Authenticity checks**: photo digest, National Register signatures of the
28
+ identity and address files, certificate chain up to the Belgian root CAs and,
29
+ on applet 1.8 cards, the chip challenge that detects cloned data.
30
+ - **Robust transport**: follows the official middleware (block size, `6Cxx`,
31
+ `61xx`, end of file), runs inside PC/SC transactions so other applications
32
+ cannot interfere, recovers when another application resets the card, and
33
+ leaves the card powered for them.
34
+ - **Typed API**: immutable dataclasses, enums and `datetime.date`, shipped
35
+ with `py.typed`; strict mypy clean.
36
+ - **Command line**: `pythonbeid read`, `pythonbeid verify`...
37
+ - No PIN is ever sent: only public card content is read.
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ pip install pythonbeid # reading
43
+ pip install "pythonbeid[verify]" # reading + signature verification
44
+ ```
45
+
46
+ Requirements: Python 3.11 or later and a PC/SC reader.
47
+
48
+ - **Windows, macOS**: nothing else to install.
49
+ - **Linux**: the PC/SC daemon must run, and [pyscard](https://pypi.org/project/pyscard/)
50
+ is compiled at install time:
51
+
52
+ ```bash
53
+ sudo apt install pcscd libpcsclite-dev # Debian, Ubuntu, Mint, MX Linux...
54
+ ```
55
+
56
+ ## Reading a card
57
+
58
+ `CardReader()` picks the first reader that holds a Belgian eID card. You can
59
+ also choose the reader and wait for the card to be inserted:
60
+
61
+ ```python
62
+ from pythonbeid import CardReader, list_readers
63
+
64
+ print(list_readers()) # ['ACS ACR38U 0', ...]
65
+
66
+ with CardReader("ACR38U", timeout=30) as reader: # index, name or part of it
67
+ identity = reader.read_identity()
68
+ address = reader.read_address()
69
+ photo = reader.read_photo() # JPEG bytes
70
+ print(reader.card_info.applet_version_string) # "1.7" or "1.8"
71
+ ```
72
+
73
+ `reader.read()` reads everything in a single transaction and returns an
74
+ `EidData` object (`card`, `identity`, `address`, `photo`, raw `files`). Pass
75
+ `photo=False` or `integrity=False` to skip the photo or the files used by the
76
+ verification. All models offer `to_dict()` for JSON serialisation.
77
+
78
+ Reading speed depends on the reader: `read()` takes about 9 s on an old
79
+ 9600-baud ACS ACR38U and proportionally less on faster readers. The photo
80
+ (3 KB) is the largest file; skip it when you do not need it.
81
+
82
+ ### Identity fields
83
+
84
+ | Field | Type | Notes |
85
+ |---|---|---|
86
+ | `card_number`, `chip_number` | `str` | chip number in hexadecimal |
87
+ | `validity_start`, `validity_end` | `date` | `is_valid_on(day)` helper |
88
+ | `issuing_municipality` | `str` | |
89
+ | `national_number` | `str` | `national_number_valid` checks the modulo-97 checksum |
90
+ | `last_name` | `str` | |
91
+ | `first_names` | `str` | the first two given names only |
92
+ | `third_given_name_initial` | `str` | `given_names` joins both |
93
+ | `nationality`, `birth_place` | `str` | in the language of the card |
94
+ | `birth_date` | `PartialDate \| None` | `.to_date()`; year or month may be unknown; `None` for an unknown format (raw value in `raw[0x0C]`) |
95
+ | `sex` | `Sex \| None` | `Sex.MALE` / `Sex.FEMALE` |
96
+ | `noble_condition` | `str` | |
97
+ | `document_type` | `DocumentType \| None` | `.description`, `.is_kids_card`, `.is_foreigner_card` |
98
+ | `special_status` | `SpecialStatus` | `.white_cane`, `.yellow_cane`, `.extended_minority` |
99
+ | `photo_hash`, `basic_key_hash` | `bytes` | used by the verification |
100
+ | foreign nationals | | `duplicate`, `special_organization`, `member_of_family`, `date_and_country_of_protection`, `work_permit`, `employer_vat_1/2`, `regional_file_number`, `brexit_mention_1/2`, `card_a_mention_1/2`, `eu_registration_date` |
101
+ | `raw` | `Mapping[int, bytes]` | every TLV record, for future tags |
102
+
103
+ The address has `street` (street and house number), `postal_code` and
104
+ `municipality`.
105
+
106
+ ## Verifying authenticity
107
+
108
+ Requires the `verify` extra.
109
+
110
+ ```python
111
+ with CardReader() as reader:
112
+ report = reader.verify()
113
+ print(report.is_valid)
114
+ for error in report.errors:
115
+ print(error)
116
+ ```
117
+
118
+ | Check | Meaning |
119
+ |---|---|
120
+ | `photo_hash_valid` | the photo matches the digest in the identity file |
121
+ | `identity_signature_valid` | the identity file is signed by the National Register (RRN) |
122
+ | `address_signature_valid` | the address file is signed by the RRN (and bound to the identity signature) |
123
+ | `rrn_certificate_trusted` | the RRN certificate is an end-entity signing certificate issued by a bundled Belgian root CA |
124
+ | `rrn_certificate_period_valid` | the RRN certificate validity overlaps the card validity |
125
+ | `content_matches_files` | the decoded identity, address and photo are those of the signed files |
126
+ | `basic_key_valid` | applet 1.8: the chip public key is the one signed by the RRN |
127
+ | `chip_authenticated` | applet 1.8: the chip signed a random challenge with its private key |
128
+
129
+ A check that does not apply is `None`. The RRN certificate is not required to
130
+ be valid today: certificates of valid cards may have expired since, and cards
131
+ re-signed after issuance carry a newer one. Whether a card has a basic key is
132
+ decided by the signed identity file, never by the unsigned chip information,
133
+ so a clone cannot avoid the chip challenge by claiming an older applet.
134
+
135
+ Raw files can be checked offline, for instance on a server receiving them
136
+ from a client:
137
+
138
+ ```python
139
+ from pythonbeid import EidData, verify_eid
140
+
141
+ eid = EidData.from_files(files) # {CardFile: bytes}
142
+ report = verify_eid(eid) # everything is checked on the raw files
143
+ ```
144
+
145
+ The chip challenge needs the live card: `reader.authenticate_chip(identity)`
146
+ checks the chip against an identity file whose signature you verified.
147
+
148
+ Bundled trust anchors, downloaded from `certs.eid.belgium.be` and
149
+ `crt.eidpki.belgium.be` (SHA-256):
150
+
151
+ | Root | Fingerprint |
152
+ |---|---|
153
+ | Belgium Root CA2 | `9f9744463be13714754e1a3becf98c08cc205e4ab32028f4e2830c4a1b2775b8` |
154
+ | Belgium Root CA3 | `a8d14e945e3e5156bcae5e39737cf6a1b1f51028bbbf982f50ce5f4c05568b4d` |
155
+ | Belgium Root CA4 | `702dd5c1a093cf0a9d71fadd9bf9a7c5857d89fb73b716e867228b3c2beb968f` |
156
+ | Belgium Root CA6 | `9c872bc979a7c09a58d4a274c199e5cb16cfa9b9618d98bc9a9988e984b8495c` |
157
+
158
+ Pass `trusted_roots=[...]` (PEM or DER) to use your own.
159
+
160
+ ## Command line
161
+
162
+ ```console
163
+ $ pythonbeid readers
164
+ 0: ACS ACR38U 0
165
+ $ pythonbeid info # reader, ATR, applet version
166
+ $ pythonbeid read --json # identity and address
167
+ $ pythonbeid read --photo me.jpg
168
+ $ pythonbeid verify # exit status 3 when a check fails
169
+ ```
170
+
171
+ `python -m pythonbeid` works too. Options: `--reader`, `--timeout`, `--json`,
172
+ `-v` / `-vv` (APDU trace, without card data).
173
+
174
+ ## Errors
175
+
176
+ All exceptions derive from `pythonbeid.BeidError`:
177
+
178
+ | Exception | Raised when |
179
+ |---|---|
180
+ | `NoReaderError` | no reader, or none matches the requested one |
181
+ | `PCSCServiceError` | the PC/SC service is not running (subclass of `NoReaderError`) |
182
+ | `NoCardError` | no card in the reader, or the card was removed |
183
+ | `UnsupportedCardError` | the card is not a Belgian eID card, or lacks a feature |
184
+ | `CardCommunicationError` | transport failure |
185
+ | `APDUError` | the card refused a command (`.sw` holds the status word) |
186
+ | `CardDataError` | card content cannot be decoded (also a `ValueError`) |
187
+
188
+ ## Migrating from 0.x
189
+
190
+ The 0.x API still works and emits a `DeprecationWarning`:
191
+
192
+ | 0.x | 1.0 |
193
+ |---|---|
194
+ | `cr.read_informations(photo=True)` | `reader.read()` returning typed `EidData` |
195
+ | `CardReader(reader_index=1)` | `CardReader(1)` or `CardReader("name")` |
196
+ | `info["birth_date"]` (`datetime`) | `identity.birth_date` (`PartialDate`) |
197
+ | `info["suffix"]` | `identity.third_given_name_initial` |
198
+ | `info["address"]`, `info["city"]` | `address.street`, `address.municipality` |
199
+ | `info["photo"]` (base-64 `str`) | `eid.photo` (`bytes`) |
200
+ | `parse_tlv`, `parse_french_date` | `decode_tlv`, `parse_birth_date` |
201
+
202
+ Behaviour fixes that may be visible: birth dates written in Dutch or German
203
+ are now parsed correctly (0.x silently returned January for most months, and
204
+ failed on German cards); values are stripped of padding spaces; unknown month
205
+ names raise instead of defaulting to January.
206
+
207
+ ## Compatibility
208
+
209
+ - Applet 1.7 cards (RSA keys): validated on real cards.
210
+ - Applet 1.8 cards (elliptic-curve keys, current generation): implemented
211
+ after the official specification and middleware, and covered by simulated
212
+ tests. Reports from 1.8 card holders are welcome (see the issue template;
213
+ never share personal data).
214
+ - Python 3.11 to 3.15 on Windows, macOS and Linux.
215
+
216
+ ## Privacy
217
+
218
+ Card content is personal data (GDPR). pythonbeid never logs it: debug logs
219
+ only contain command headers, status words and lengths. Ask for consent and
220
+ keep only what you need.
221
+
222
+ ## Development
223
+
224
+ ```bash
225
+ git clone https://github.com/Lapin-Blanc/pythonbeid && cd pythonbeid
226
+ python -m venv .venv && . .venv/bin/activate # .venv\Scripts\activate on Windows
227
+ pip install -e ".[dev]" && pre-commit install
228
+ pytest # simulated card, no hardware needed
229
+ pytest --hardware # also runs the tests on a real card (no data is printed)
230
+ ```
231
+
232
+ See [CONTRIBUTING.md](CONTRIBUTING.md).
233
+
234
+ ## References
235
+
236
+ Official documentation, in the
237
+ [Fedict/eid-mw](https://github.com/Fedict/eid-mw/tree/master/doc/sdk/documentation)
238
+ repository:
239
+
240
+ - *ID and Address file contents*, applet 1.8, v5.0 (2023)
241
+ - *Belgian Electronic Identity Card content*, v5.4
242
+ - *Reference manual Belpic V1.8 eID card* and *V1.7*
243
+
244
+ ## License
245
+
246
+ MIT, see [LICENSE](LICENSE).