pythonbeid 0.2.0__py3-none-any.whl → 1.0.0__py3-none-any.whl
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.
- pythonbeid/__init__.py +77 -6
- pythonbeid/__main__.py +3 -0
- pythonbeid/card_reader.py +846 -256
- pythonbeid/cli.py +176 -0
- pythonbeid/exceptions.py +73 -30
- pythonbeid/integrity.py +455 -0
- pythonbeid/models.py +593 -0
- pythonbeid/parser.py +472 -89
- pythonbeid/py.typed +0 -0
- pythonbeid/trust/belgium_root_ca2.pem +22 -0
- pythonbeid/trust/belgium_root_ca3.pem +32 -0
- pythonbeid/trust/belgium_root_ca4.pem +32 -0
- pythonbeid/trust/belgium_root_ca6.pem +21 -0
- pythonbeid-1.0.0.dist-info/METADATA +293 -0
- pythonbeid-1.0.0.dist-info/RECORD +19 -0
- {pythonbeid-0.2.0.dist-info → pythonbeid-1.0.0.dist-info}/WHEEL +1 -1
- pythonbeid-1.0.0.dist-info/entry_points.txt +2 -0
- {pythonbeid-0.2.0.dist-info → pythonbeid-1.0.0.dist-info}/licenses/LICENSE +21 -21
- pythonbeid-0.2.0.dist-info/METADATA +0 -104
- pythonbeid-0.2.0.dist-info/RECORD +0 -9
- {pythonbeid-0.2.0.dist-info → pythonbeid-1.0.0.dist-info}/top_level.txt +0 -0
pythonbeid/parser.py
CHANGED
|
@@ -1,89 +1,472 @@
|
|
|
1
|
-
"""Pure
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
""
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
1
|
+
"""Pure decoding of Belgian eID file contents.
|
|
2
|
+
|
|
3
|
+
Nothing in this module talks to a card: every function takes the raw bytes of
|
|
4
|
+
a file and returns typed values, so the parsing can be reused with any
|
|
5
|
+
transport (PC/SC, PKCS#11 raw files, test fixtures...).
|
|
6
|
+
|
|
7
|
+
Reference: *ID and Address file contents* v5.0 and *Belgian Electronic
|
|
8
|
+
Identity Card content* v5.4 (``Fedict/eid-mw``, ``doc/sdk/documentation``).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import logging
|
|
14
|
+
import re
|
|
15
|
+
import unicodedata
|
|
16
|
+
import warnings
|
|
17
|
+
from datetime import date, datetime
|
|
18
|
+
from types import MappingProxyType
|
|
19
|
+
from typing import TYPE_CHECKING
|
|
20
|
+
|
|
21
|
+
from .exceptions import CardDataError
|
|
22
|
+
from .models import (
|
|
23
|
+
Address,
|
|
24
|
+
CardInfo,
|
|
25
|
+
DocumentType,
|
|
26
|
+
Identity,
|
|
27
|
+
PartialDate,
|
|
28
|
+
Sex,
|
|
29
|
+
SpecialStatus,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
if TYPE_CHECKING:
|
|
33
|
+
from collections.abc import Mapping
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"decode_tlv",
|
|
37
|
+
"is_valid_national_number",
|
|
38
|
+
"parse_address",
|
|
39
|
+
"parse_birth_date",
|
|
40
|
+
"parse_card_data",
|
|
41
|
+
"parse_card_date",
|
|
42
|
+
"parse_french_date",
|
|
43
|
+
"parse_identity",
|
|
44
|
+
"parse_tlv",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
logger = logging.getLogger(__name__)
|
|
48
|
+
|
|
49
|
+
# Month names found on cards, in French, Dutch, German (diacritics removed,
|
|
50
|
+
# so "MÄR" becomes "MAR") plus English and legacy pythonbeid aliases.
|
|
51
|
+
_MONTHS: dict[str, int] = {
|
|
52
|
+
"JAN": 1, "JANV": 1,
|
|
53
|
+
"FEV": 2, "FEVR": 2, "FEB": 2,
|
|
54
|
+
"MAR": 3, "MARS": 3, "MAAR": 3,
|
|
55
|
+
"AVR": 4, "AVRI": 4, "APR": 4,
|
|
56
|
+
"MAI": 5, "MEI": 5, "MAY": 5,
|
|
57
|
+
"JUN": 6, "JUIN": 6,
|
|
58
|
+
"JUL": 7, "JUIL": 7,
|
|
59
|
+
"AOU": 8, "AOUT": 8, "AUG": 8,
|
|
60
|
+
"SEP": 9, "SEPT": 9,
|
|
61
|
+
"OCT": 10, "OCTO": 10, "OKT": 10,
|
|
62
|
+
"NOV": 11, "NOVE": 11,
|
|
63
|
+
"DEC": 12, "DECE": 12, "DEZ": 12,
|
|
64
|
+
} # fmt: skip
|
|
65
|
+
|
|
66
|
+
_FEMALE_LETTERS = frozenset("FVW") # French, Dutch (vrouw), German (weiblich)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# ── TLV ───────────────────────────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def decode_tlv(data: bytes) -> Mapping[int, bytes]:
|
|
73
|
+
"""Decode a TLV file of the Belgian eID card.
|
|
74
|
+
|
|
75
|
+
The format is the one of the specification: a one-byte tag, a length
|
|
76
|
+
encoded on one byte (or ``FF`` continuation bytes for 255 and more,
|
|
77
|
+
``FF 05`` meaning 260) and the value. Tag ``0x00`` (file structure
|
|
78
|
+
version) may only appear first; a zero byte found later is the padding
|
|
79
|
+
that ends the useful content.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
data: Raw content of the file.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
A read-only mapping from tag to value, in file order.
|
|
86
|
+
|
|
87
|
+
Raises:
|
|
88
|
+
CardDataError: If a record is truncated.
|
|
89
|
+
"""
|
|
90
|
+
records: dict[int, bytes] = {}
|
|
91
|
+
index, size = 0, len(data)
|
|
92
|
+
while index < size:
|
|
93
|
+
tag = data[index]
|
|
94
|
+
if tag == 0x00 and index > 0:
|
|
95
|
+
break # zero padding
|
|
96
|
+
index += 1
|
|
97
|
+
length = 0
|
|
98
|
+
while True:
|
|
99
|
+
if index >= size:
|
|
100
|
+
msg = f"truncated TLV length for tag 0x{tag:02X}"
|
|
101
|
+
raise CardDataError(msg)
|
|
102
|
+
byte = data[index]
|
|
103
|
+
index += 1
|
|
104
|
+
length += byte
|
|
105
|
+
if byte != 0xFF:
|
|
106
|
+
break
|
|
107
|
+
if index + length > size:
|
|
108
|
+
msg = (
|
|
109
|
+
f"truncated TLV value for tag 0x{tag:02X} "
|
|
110
|
+
f"({length} bytes announced, {size - index} available)"
|
|
111
|
+
)
|
|
112
|
+
raise CardDataError(msg)
|
|
113
|
+
if tag in records:
|
|
114
|
+
logger.warning("Duplicate TLV tag 0x%02X ignored", tag)
|
|
115
|
+
else:
|
|
116
|
+
records[tag] = data[index : index + length]
|
|
117
|
+
index += length
|
|
118
|
+
return MappingProxyType(records)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _text(value: bytes | None) -> str:
|
|
122
|
+
"""Decode a UTF-8 card field, trimming spaces and zero padding."""
|
|
123
|
+
if not value:
|
|
124
|
+
return ""
|
|
125
|
+
try:
|
|
126
|
+
text = value.decode("utf-8")
|
|
127
|
+
except UnicodeDecodeError:
|
|
128
|
+
logger.warning("Invalid UTF-8 in card field, replacing undecodable bytes")
|
|
129
|
+
text = value.decode("utf-8", errors="replace")
|
|
130
|
+
return text.strip("\x00").strip()
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _optional_text(records: Mapping[int, bytes], tag: int) -> str | None:
|
|
134
|
+
if tag not in records:
|
|
135
|
+
return None
|
|
136
|
+
return _text(records[tag]) or None
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _optional_int(records: Mapping[int, bytes], tag: int) -> int | None:
|
|
140
|
+
text = _optional_text(records, tag)
|
|
141
|
+
if text is None:
|
|
142
|
+
return None
|
|
143
|
+
if not text.isdigit():
|
|
144
|
+
logger.warning("Non-numeric value in tag 0x%02X ignored", tag)
|
|
145
|
+
return None
|
|
146
|
+
return int(text)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _version(records: Mapping[int, bytes]) -> int | None:
|
|
150
|
+
value = records.get(0x00)
|
|
151
|
+
return int.from_bytes(value, "big") if value else None
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _der_length(data: bytes) -> int:
|
|
155
|
+
"""Total length of the DER SEQUENCE at the start of *data*.
|
|
156
|
+
|
|
157
|
+
Certificates, signatures and keys are stored in files larger than the
|
|
158
|
+
object itself, padded with zero bytes.
|
|
159
|
+
|
|
160
|
+
Raises:
|
|
161
|
+
CardDataError: If *data* does not start with a valid DER header.
|
|
162
|
+
"""
|
|
163
|
+
if len(data) < 2:
|
|
164
|
+
msg = "DER object too short"
|
|
165
|
+
raise CardDataError(msg)
|
|
166
|
+
if data[0] != 0x30:
|
|
167
|
+
msg = "not a DER SEQUENCE (empty or invalid file)"
|
|
168
|
+
raise CardDataError(msg)
|
|
169
|
+
first = data[1]
|
|
170
|
+
if first < 0x80:
|
|
171
|
+
total = 2 + first
|
|
172
|
+
else:
|
|
173
|
+
count = first & 0x7F
|
|
174
|
+
if count == 0 or count > 4 or len(data) < 2 + count:
|
|
175
|
+
msg = "invalid DER length"
|
|
176
|
+
raise CardDataError(msg)
|
|
177
|
+
total = 2 + count + int.from_bytes(data[2 : 2 + count], "big")
|
|
178
|
+
if total > len(data):
|
|
179
|
+
msg = f"truncated DER object ({total} bytes announced, {len(data)} available)"
|
|
180
|
+
raise CardDataError(msg)
|
|
181
|
+
return total
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# ── Dates ─────────────────────────────────────────────────────────────────
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def parse_card_date(text: str) -> date:
|
|
188
|
+
"""Parse a ``DD.MM.YYYY`` date (validity dates of the card).
|
|
189
|
+
|
|
190
|
+
Raises:
|
|
191
|
+
CardDataError: If the text is not a valid date in that format.
|
|
192
|
+
"""
|
|
193
|
+
try:
|
|
194
|
+
return datetime.strptime(text.strip(), "%d.%m.%Y").date() # noqa: DTZ007
|
|
195
|
+
except ValueError as exc:
|
|
196
|
+
msg = "invalid card date (DD.MM.YYYY expected)"
|
|
197
|
+
raise CardDataError(msg) from exc
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _month(token: str) -> int:
|
|
201
|
+
plain = "".join(
|
|
202
|
+
char
|
|
203
|
+
for char in unicodedata.normalize("NFKD", token.upper())
|
|
204
|
+
if not unicodedata.combining(char)
|
|
205
|
+
)
|
|
206
|
+
try:
|
|
207
|
+
return _MONTHS[plain]
|
|
208
|
+
except KeyError:
|
|
209
|
+
msg = "unknown month abbreviation in birth date"
|
|
210
|
+
raise CardDataError(msg) from None
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def parse_birth_date(text: str) -> PartialDate:
|
|
214
|
+
"""Parse the localised birth date of the identity file.
|
|
215
|
+
|
|
216
|
+
Supported layouts, as written by the issuing municipality:
|
|
217
|
+
|
|
218
|
+
* French and Dutch: ``DD MMMM YYYY`` (``"12 MEI 1990"``, ``"26 AVR 1975"``)
|
|
219
|
+
* German: ``DD.MMM.YYYY`` or ``DD.MMM. YYYY`` (``"22.DEZ. 1997"``)
|
|
220
|
+
* Incomplete dates: blank day and/or month (``" 1984"``,
|
|
221
|
+
``" MARS 1950"``)
|
|
222
|
+
|
|
223
|
+
Args:
|
|
224
|
+
text: Value of tag ``0x0C``.
|
|
225
|
+
|
|
226
|
+
Returns:
|
|
227
|
+
The date, possibly without day and month.
|
|
228
|
+
|
|
229
|
+
Raises:
|
|
230
|
+
CardDataError: If the text cannot be interpreted. Unknown month
|
|
231
|
+
names are rejected instead of being guessed. The message never
|
|
232
|
+
contains the date itself, which is personal data.
|
|
233
|
+
"""
|
|
234
|
+
tokens = text.replace(".", " ").split()
|
|
235
|
+
try:
|
|
236
|
+
match tokens:
|
|
237
|
+
case [year]:
|
|
238
|
+
result = PartialDate(_year(year))
|
|
239
|
+
case [first, year] if first.isdigit():
|
|
240
|
+
# A day without a month carries no information.
|
|
241
|
+
result = PartialDate(_year(year))
|
|
242
|
+
case [month, year]:
|
|
243
|
+
result = PartialDate(_year(year), _month(month))
|
|
244
|
+
case [day, month, year] if day.isdigit():
|
|
245
|
+
day_value = int(day)
|
|
246
|
+
result = PartialDate(_year(year), _month(month), day_value or None)
|
|
247
|
+
case _:
|
|
248
|
+
msg = "unexpected birth date format"
|
|
249
|
+
raise CardDataError(msg)
|
|
250
|
+
except CardDataError:
|
|
251
|
+
raise
|
|
252
|
+
except ValueError as exc:
|
|
253
|
+
msg = "invalid birth date"
|
|
254
|
+
raise CardDataError(msg) from exc
|
|
255
|
+
return result
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _year(token: str) -> int:
|
|
259
|
+
if not re.fullmatch(r"\d{4}", token):
|
|
260
|
+
msg = "invalid year in birth date"
|
|
261
|
+
raise CardDataError(msg)
|
|
262
|
+
return int(token)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
# ── Field helpers ─────────────────────────────────────────────────────────
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _sex(value: str) -> Sex | None:
|
|
269
|
+
letter = value.upper()
|
|
270
|
+
if letter == "M":
|
|
271
|
+
return Sex.MALE
|
|
272
|
+
if letter in _FEMALE_LETTERS:
|
|
273
|
+
return Sex.FEMALE
|
|
274
|
+
logger.warning("Unexpected value in the sex field (tag 0x0D) ignored")
|
|
275
|
+
return None
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _document_type(records: Mapping[int, bytes]) -> DocumentType | None:
|
|
279
|
+
code = _optional_int(records, 0x0F)
|
|
280
|
+
return None if code is None else DocumentType(code)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _special_status(records: Mapping[int, bytes]) -> SpecialStatus:
|
|
284
|
+
code = _optional_int(records, 0x10)
|
|
285
|
+
return SpecialStatus(code or 0)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def _optional_card_date(records: Mapping[int, bytes], tag: int) -> date | None:
|
|
289
|
+
text = _optional_text(records, tag)
|
|
290
|
+
if text is None:
|
|
291
|
+
return None
|
|
292
|
+
try:
|
|
293
|
+
return parse_card_date(text)
|
|
294
|
+
except CardDataError:
|
|
295
|
+
logger.warning("Invalid date in tag 0x%02X ignored", tag)
|
|
296
|
+
return None
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def _birth_date(text: str) -> PartialDate | None:
|
|
300
|
+
try:
|
|
301
|
+
return parse_birth_date(text)
|
|
302
|
+
except CardDataError as exc:
|
|
303
|
+
logger.warning("Birth date not decoded (%s); see Identity.raw[0x0C]", exc)
|
|
304
|
+
return None
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _required(records: Mapping[int, bytes], tag: int, name: str) -> str:
|
|
308
|
+
if tag not in records:
|
|
309
|
+
msg = f"identity file lacks the {name} (tag 0x{tag:02X})"
|
|
310
|
+
raise CardDataError(msg)
|
|
311
|
+
return _text(records[tag])
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def is_valid_national_number(national_number: str) -> bool:
|
|
315
|
+
"""Check the modulo-97 checksum of a Belgian national register number.
|
|
316
|
+
|
|
317
|
+
Separators (dots, dashes, spaces) are ignored. People born in 2000 or
|
|
318
|
+
later use the same rule with a ``2`` prefixed to the first nine digits.
|
|
319
|
+
"""
|
|
320
|
+
digits = re.sub(r"[\s.\-]", "", national_number)
|
|
321
|
+
if not re.fullmatch(r"\d{11}", digits):
|
|
322
|
+
return False
|
|
323
|
+
check = int(digits[9:])
|
|
324
|
+
return check in (97 - int(digits[:9]) % 97, 97 - int("2" + digits[:9]) % 97)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
# ── Files ─────────────────────────────────────────────────────────────────
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def parse_identity(data: bytes) -> Identity:
|
|
331
|
+
"""Decode the identity file (``DF01/4031``).
|
|
332
|
+
|
|
333
|
+
Raises:
|
|
334
|
+
CardDataError: If a mandatory field is missing or malformed.
|
|
335
|
+
"""
|
|
336
|
+
records = decode_tlv(data)
|
|
337
|
+
return Identity(
|
|
338
|
+
card_number=_required(records, 0x01, "card number"),
|
|
339
|
+
chip_number=records.get(0x02, b"").hex().upper(),
|
|
340
|
+
validity_start=parse_card_date(_required(records, 0x03, "validity start")),
|
|
341
|
+
validity_end=parse_card_date(_required(records, 0x04, "validity end")),
|
|
342
|
+
issuing_municipality=_text(records.get(0x05)),
|
|
343
|
+
national_number=_required(records, 0x06, "national number"),
|
|
344
|
+
last_name=_required(records, 0x07, "last name"),
|
|
345
|
+
first_names=_text(records.get(0x08)),
|
|
346
|
+
third_given_name_initial=_text(records.get(0x09)),
|
|
347
|
+
nationality=_text(records.get(0x0A)),
|
|
348
|
+
birth_place=_text(records.get(0x0B)),
|
|
349
|
+
birth_date=_birth_date(_required(records, 0x0C, "birth date")),
|
|
350
|
+
sex=_sex(_text(records.get(0x0D))),
|
|
351
|
+
noble_condition=_text(records.get(0x0E)),
|
|
352
|
+
document_type=_document_type(records),
|
|
353
|
+
special_status=_special_status(records),
|
|
354
|
+
photo_hash=bytes(records.get(0x11, b"")),
|
|
355
|
+
duplicate=_optional_int(records, 0x12),
|
|
356
|
+
special_organization=_optional_text(records, 0x13),
|
|
357
|
+
member_of_family=0x14 in records,
|
|
358
|
+
date_and_country_of_protection=_optional_text(records, 0x15),
|
|
359
|
+
work_permit=_optional_text(records, 0x16),
|
|
360
|
+
employer_vat_1=_optional_text(records, 0x17),
|
|
361
|
+
employer_vat_2=_optional_text(records, 0x18),
|
|
362
|
+
regional_file_number=_optional_text(records, 0x19),
|
|
363
|
+
basic_key_hash=bytes(records[0x1A]) if records.get(0x1A) else None,
|
|
364
|
+
brexit_mention_1=_optional_text(records, 0x1B),
|
|
365
|
+
brexit_mention_2=_optional_text(records, 0x1C),
|
|
366
|
+
card_a_mention_1=_optional_text(records, 0x1D),
|
|
367
|
+
card_a_mention_2=_optional_text(records, 0x1E),
|
|
368
|
+
eu_registration_date=_optional_card_date(records, 0x1F),
|
|
369
|
+
file_structure_version=_version(records),
|
|
370
|
+
raw=records,
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
def parse_address(data: bytes) -> Address:
|
|
375
|
+
"""Decode the address file (``DF01/4033``).
|
|
376
|
+
|
|
377
|
+
Raises:
|
|
378
|
+
CardDataError: If the file is malformed.
|
|
379
|
+
"""
|
|
380
|
+
records = decode_tlv(data)
|
|
381
|
+
return Address(
|
|
382
|
+
street=_text(records.get(0x01)),
|
|
383
|
+
postal_code=_text(records.get(0x02)),
|
|
384
|
+
municipality=_text(records.get(0x03)),
|
|
385
|
+
file_structure_version=_version(records),
|
|
386
|
+
raw=records,
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def parse_card_data(data: bytes, extended: bytes | None = None) -> CardInfo:
|
|
391
|
+
"""Decode the answer of GET CARD DATA (``80 E4 00 00 1C``).
|
|
392
|
+
|
|
393
|
+
Args:
|
|
394
|
+
data: The 28 bytes returned with ``P2=00``.
|
|
395
|
+
extended: The 31 bytes returned with ``P2=01`` (applet 1.8 only),
|
|
396
|
+
which add the number of remaining PIN attempts.
|
|
397
|
+
|
|
398
|
+
Raises:
|
|
399
|
+
CardDataError: If the answer is too short.
|
|
400
|
+
"""
|
|
401
|
+
if len(data) < 28:
|
|
402
|
+
msg = f"card data too short: {len(data)} bytes, 28 expected"
|
|
403
|
+
raise CardDataError(msg)
|
|
404
|
+
pin_attempts: int | None = None
|
|
405
|
+
if extended is not None and len(extended) > 28 and extended[28] != 0xFF:
|
|
406
|
+
pin_attempts = extended[28]
|
|
407
|
+
return CardInfo(
|
|
408
|
+
serial_number=data[:16].hex().upper(),
|
|
409
|
+
component_code=data[16],
|
|
410
|
+
os_number=data[17],
|
|
411
|
+
os_version=data[18],
|
|
412
|
+
softmask_number=data[19],
|
|
413
|
+
softmask_version=data[20],
|
|
414
|
+
applet_version=data[21],
|
|
415
|
+
global_os_version=int.from_bytes(data[22:24], "big"),
|
|
416
|
+
applet_interface_version=data[24],
|
|
417
|
+
pkcs1_support=data[25],
|
|
418
|
+
key_exchange_version=data[26],
|
|
419
|
+
lifecycle=data[27],
|
|
420
|
+
pin_attempts_left=pin_attempts,
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
# ── Deprecated 0.x API ────────────────────────────────────────────────────
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def parse_tlv(data: list[int], num_fields: int) -> list[str]:
|
|
428
|
+
"""Return the first *num_fields* TLV values, ignoring their tags.
|
|
429
|
+
|
|
430
|
+
.. deprecated:: 1.0
|
|
431
|
+
Use :func:`decode_tlv`, which keys values by tag and follows the
|
|
432
|
+
specification (length continuation, padding).
|
|
433
|
+
"""
|
|
434
|
+
warnings.warn(
|
|
435
|
+
"parse_tlv() is deprecated, use decode_tlv() instead",
|
|
436
|
+
DeprecationWarning,
|
|
437
|
+
stacklevel=2,
|
|
438
|
+
)
|
|
439
|
+
index, values = 0, list[str]()
|
|
440
|
+
while len(values) < num_fields and index + 1 < len(data):
|
|
441
|
+
length = data[index + 1]
|
|
442
|
+
index += 2
|
|
443
|
+
if index + length > len(data):
|
|
444
|
+
break
|
|
445
|
+
raw = bytes(data[index : index + length])
|
|
446
|
+
index += length
|
|
447
|
+
try:
|
|
448
|
+
values.append(raw.decode("utf-8"))
|
|
449
|
+
except UnicodeDecodeError:
|
|
450
|
+
values.append("")
|
|
451
|
+
return values
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def parse_french_date(date_str: str) -> datetime:
|
|
455
|
+
"""Parse a complete birth date into a naive :class:`datetime`.
|
|
456
|
+
|
|
457
|
+
.. deprecated:: 1.0
|
|
458
|
+
Use :func:`parse_birth_date`, which also supports incomplete dates.
|
|
459
|
+
Despite its name this function accepts French, Dutch and German
|
|
460
|
+
month names; unknown months now raise instead of defaulting to
|
|
461
|
+
January.
|
|
462
|
+
|
|
463
|
+
Raises:
|
|
464
|
+
ValueError: If the date is invalid or incomplete.
|
|
465
|
+
"""
|
|
466
|
+
warnings.warn(
|
|
467
|
+
"parse_french_date() is deprecated, use parse_birth_date() instead",
|
|
468
|
+
DeprecationWarning,
|
|
469
|
+
stacklevel=2,
|
|
470
|
+
)
|
|
471
|
+
parsed = parse_birth_date(date_str).to_date()
|
|
472
|
+
return datetime(parsed.year, parsed.month, parsed.day) # noqa: DTZ001
|
pythonbeid/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
-----BEGIN CERTIFICATE-----
|
|
2
|
+
MIIDjjCCAnagAwIBAgIIKv++n6Lw6YcwDQYJKoZIhvcNAQEFBQAwKDELMAkGA1UE
|
|
3
|
+
BhMCQkUxGTAXBgNVBAMTEEJ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