-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.py
561 lines (413 loc) · 14 KB
/
json.py
1
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
"""A recursive descent parser for JSON TUF metadata."""
import os
import re
from typing import (
Any,
Callable,
List,
Sized,
Tuple,
Type,
)
from securesystemslib.formats import encode_canonical
from . import Parser
from ..models.common import (
DateTime,
Filepaths,
Hashes,
Json,
KeyID,
Length,
Rolename,
SpecVersion,
Version,
)
from ..models.metadata import (
Delegation,
Delegations,
ECDSAScheme,
ED25519Scheme,
Metadata,
PublicKey,
PublicKeys,
Root,
RSAScheme,
Scheme,
Signature,
Signatures,
Signed,
Snapshot,
TargetFile,
TargetFiles,
Targets,
Threshold,
ThresholdOfPublicKeys,
TimeSnap,
TimeSnaps,
Timestamp,
)
def check_dict(d: Any) -> None:
if not isinstance(d, dict):
raise TypeError(f"{d} is not a dict")
def canonical(_signed: Json) -> bytes:
"""Returns the UTF-8 encoded canonical JSON representation of _signed."""
return encode_canonical(_signed).encode("utf-8")
def check_key(observed: str, expected: str) -> None:
if observed != expected:
raise ValueError(f"{observed} != {expected}")
def spec_version(sv: str) -> SpecVersion:
_spec_version = SpecVersion(sv)
if _spec_version.major != 1:
raise ValueError(f"unsupported major spec_version: {_spec_version}")
return _spec_version
def footer(
_signed: Json, callback: Callable[[Json], Any] = lambda x: None
) -> Tuple[SpecVersion, Any, Version]:
check_dict(_signed)
k, version = _signed.popitem()
check_key(k, "version")
version = Version(version)
result = callback(_signed)
k, _spec_version = _signed.popitem()
check_key(k, "spec_version")
_spec_version = spec_version(_spec_version)
return _spec_version, result, version
def check_empty(i: Sized) -> None:
if len(i) > 0:
raise ValueError(f"{i} is not empty")
def check_str(s: Any) -> None:
if not isinstance(s, str):
raise TypeError(f"{s} is not a str")
def keyval(_keyval: Json) -> str:
check_dict(_keyval)
k, public = _keyval.popitem()
check_key(k, "public")
check_str(public)
check_empty(_keyval)
return public
def check_scheme(observed: Type[Scheme], expected: Type[Scheme]) -> None:
if observed is not expected:
raise ValueError(f"{observed} != {expected}")
def key(_key: Json) -> PublicKey:
check_dict(_key)
k, scheme = _key.popitem()
check_key(k, "scheme")
if scheme == "ecdsa-sha2-nistp256":
scheme = ECDSAScheme
elif scheme == "ed25519":
scheme = ED25519Scheme
elif scheme == "rsassa-pss-sha256":
scheme = RSAScheme
else:
raise ValueError(f"{_key} has unknown scheme: {scheme}")
k, _keyval = _key.popitem()
check_key(k, "keyval")
value = keyval(_keyval)
k, keytype = _key.popitem()
check_key(k, "keytype")
if keytype == "ecdsa-sha2-nistp256":
check_scheme(scheme, ECDSAScheme)
elif keytype == "ed25519":
check_scheme(scheme, ED25519Scheme)
elif keytype == "rsa":
check_scheme(scheme, RSAScheme)
else:
raise ValueError(f"{_key} has unknown keytype: {keytype}")
k, keyid_hash_algorithms = _key.popitem()
check_key(k, "keyid_hash_algorithms")
if keyid_hash_algorithms != ["sha256", "sha512"]:
raise ValueError(
f"{_key} has unknown keyid_hash_algorithms: {keyid_hash_algorithms}"
)
check_empty(_key)
return PublicKey(scheme, value)
def keys(_keys: Json) -> PublicKeys:
check_dict(_keys)
# NOTE: we iterate in order of appearance to preserve it in the new dict.
return {keyid: key(_key) for keyid, _key in _keys.items()}
def check_int(i: Any) -> None:
if not isinstance(i, int):
raise ValueError(f"{i} is not an int")
def check_list(_list: Any) -> None:
if not isinstance(_list, list):
raise TypeError(f"{_list} is not a list")
def role(
_role: dict, _keys: PublicKeys, callback: Callable[[Json], Any] = lambda x: None
) -> Tuple[Any, ThresholdOfPublicKeys]:
check_dict(_role)
k, threshold = _role.popitem()
check_key(k, "threshold")
check_int(threshold)
threshold = Threshold(threshold)
result = callback(_role)
k, keyids = _role.popitem()
check_key(k, "keyids")
check_list(keyids)
_keys = {keyid: _keys[keyid] for keyid in set(keyids)}
check_empty(_role)
return result, ThresholdOfPublicKeys(threshold, _keys)
def root_roles(
roles: dict, _keys: PublicKeys
) -> Tuple[
ThresholdOfPublicKeys,
ThresholdOfPublicKeys,
ThresholdOfPublicKeys,
ThresholdOfPublicKeys,
]:
check_dict(roles)
k, timestamp = roles.popitem()
check_key(k, "timestamp")
_, timestamp = role(timestamp, _keys)
k, targets = roles.popitem()
check_key(k, "targets")
_, targets = role(targets, _keys)
k, snapshot = roles.popitem()
check_key(k, "snapshot")
_, snapshot = role(snapshot, _keys)
k, root = roles.popitem()
check_key(k, "root")
_, root = role(root, _keys)
return root, snapshot, targets, timestamp
def expires(_expires: str) -> DateTime:
return DateTime.strptime(_expires, "%Y-%m-%dT%H:%M:%S%z")
def header(
_signed: Json, expected_type: str, callback: Callable[[Json], Any] = lambda x: None
) -> Tuple[Any, DateTime]:
k, _expires = _signed.popitem()
check_key(k, "expires")
_expires = expires(_expires)
result = callback(_signed)
k, _type = _signed.popitem()
check_key(k, "_type")
if _type != expected_type:
raise ValueError(f"{_signed} has unexpected type {_type} != {expected_type}")
check_empty(_signed)
return result, _expires
def check_bool(b: Any) -> None:
if not isinstance(b, bool):
raise TypeError(f"{b} is not a bool")
def root(_signed: Json) -> Root:
_spec_version, _, version = footer(_signed)
k, _root_roles = _signed.popitem()
check_key(k, "roles")
k, _keys = _signed.popitem()
check_key(k, "keys")
_keys = keys(_keys)
# TODO: is it a big deal that we do not check whether all keys listed are used?
_root, _snapshot, _targets, _timestamp = root_roles(_root_roles, _keys)
def callback(_signed: Json) -> bool:
k, consistent_snapshot = _signed.popitem()
check_key(k, "consistent_snapshot")
check_bool(consistent_snapshot)
return consistent_snapshot
consistent_snapshot, _expires = header(_signed, "root", callback=callback)
check_empty(_signed)
return Root(
_expires,
_spec_version,
version,
consistent_snapshot,
_root,
_snapshot,
_targets,
_timestamp,
)
ROLENAME_PATTERN = re.compile(r"^[0-9a-z\-]+")
def check_rolename(rolename: Rolename) -> None:
check_str(rolename)
if not ROLENAME_PATTERN.fullmatch(rolename):
raise ValueError(f"{rolename} is not a valid targets rolename")
def hashes(_hashes: Json) -> Hashes:
check_dict(_hashes)
# We don't do much here... for now.
for key, value in _hashes.items():
check_str(key)
check_str(value)
return _hashes
def meta(_meta: Json) -> TimeSnaps:
check_dict(_meta)
timesnaps: TimeSnaps = {}
# NOTE: we iterate in order of appearance to preserve it in the new dict.
for filename, timesnap in _meta.items():
rolename, _ = os.path.splitext(filename)
check_rolename(rolename)
check_dict(timesnap)
k, version = timesnap.popitem()
check_key(k, "version")
version = Version(version)
try:
k, length = timesnap.popitem()
except KeyError:
length = None
else:
check_key(k, "length")
length = Length(length)
try:
k, _hashes = timesnap.popitem()
except KeyError:
_hashes = None
else:
check_key(k, "hashes")
_hashes = hashes(_hashes)
check_empty(timesnap)
timesnaps[filename] = TimeSnap(version, _hashes, length)
return timesnaps
def timestamp(_signed: Json) -> Timestamp:
_spec_version, _, version = footer(_signed)
k, _meta = _signed.popitem()
check_key(k, "meta")
timesnaps = meta(_meta)
check_dict(timesnaps)
k, timesnap = timesnaps.popitem()
check_key(k, "snapshot.json")
check_empty(timesnaps)
_, _expires = header(_signed, "timestamp")
return Timestamp(
_expires,
_spec_version,
version,
timesnap,
)
def snapshot(_signed: Json) -> Snapshot:
_spec_version, _, version = footer(_signed)
k, _meta = _signed.popitem()
check_key(k, "meta")
timesnaps = meta(_meta)
check_dict(timesnaps)
_, _expires = header(_signed, "snapshot")
return Snapshot(
_expires,
_spec_version,
version,
timesnaps,
)
def target_file(_target_file: Json) -> TargetFile:
check_dict(_target_file)
k, length = _target_file.popitem()
check_key(k, "length")
length = Length(length)
k, _hashes = _target_file.popitem()
check_key(k, "hashes")
_hashes = hashes(_hashes)
try:
k, custom = _target_file.popitem()
except KeyError:
custom = None
else:
check_dict(custom)
check_empty(_target_file)
return TargetFile(_hashes, length, custom)
def target_files(_target_files: Json) -> TargetFiles:
check_dict(_target_files)
# NOTE: we iterate in order of appearance to preserve it in the new dict.
return {path: target_file(file) for path, file in _target_files.items()}
# One or more of anything in [a-zA-Z0-9_], a hypen, an asterisk, or a dot.
_FILENAME_PATTERN = r"[\w\-*.]+"
# One or more filenames, separated by a forward slash.
TARGETS_PATH_PATTERN = re.compile(
fr"^{_FILENAME_PATTERN}(?:/{_FILENAME_PATTERN})*$", re.ASCII
)
def targets_roles(roles: dict, _keys: PublicKeys) -> Delegations:
check_list(roles)
delegations: Delegations = {}
def callback(_role: dict) -> Tuple[Rolename, Filepaths, bool]:
k, terminating = _role.popitem()
check_key(k, "terminating")
check_bool(terminating)
k, paths = _role.popitem()
check_key(k, "paths")
check_list(paths)
for path in paths:
if not TARGETS_PATH_PATTERN.fullmatch(path):
raise ValueError(f"{path} is not a valid targets path pattern")
k, rolename = _role.popitem()
check_key(k, "name")
check_rolename(rolename)
return rolename, paths, terminating
# NOTE: we iterate in order of appearance to preserve it in the new dict.
while len(roles) > 0:
_role = roles.pop(0)
rolename: Rolename
paths: Filepaths
terminating: bool
result, _role = role(_role, _keys, callback=callback)
rolename, paths, terminating = result
if rolename in delegations:
raise ValueError(f"{roles} has duplicate {rolename}")
delegations[rolename] = Delegation(_role, paths, terminating)
return delegations
def targets(_signed: Json) -> Targets:
def get_target_files(_signed: Json) -> TargetFiles:
k, _target_files = _signed.popitem()
check_key(k, "targets")
return target_files(_target_files)
_spec_version, _target_files, version = footer(_signed, callback=get_target_files)
def get_delegations(_signed: Json) -> Delegations:
k, delegations = _signed.popitem()
check_key(k, "delegations")
check_dict(delegations)
k, _targets_roles = delegations.popitem()
check_key(k, "roles")
k, _keys = delegations.popitem()
check_key(k, "keys")
_keys = keys(_keys)
check_empty(delegations)
return targets_roles(_targets_roles, _keys)
delegations, _expires = header(_signed, "targets", callback=get_delegations)
return Targets(_expires, _spec_version, version, _target_files, delegations)
def signed(_signed: Json) -> Signed:
# Peek ahead.
type = _signed.get("_type")
if not type:
raise TypeError(f"{_signed} has no type")
if type == "root":
return root(_signed)
elif type == "timestamp":
return timestamp(_signed)
elif type == "snapshot":
return snapshot(_signed)
elif type == "targets":
return targets(_signed)
else:
raise ValueError(f"{_signed} has unknown type {type}")
def signature(_signature: Json) -> Tuple[KeyID, Signature]:
check_dict(_signature)
k, sig = _signature.popitem()
check_key(k, "sig")
check_str(sig)
k, keyid = _signature.popitem()
check_key(k, "keyid")
check_str(keyid)
check_empty(_signature)
return keyid, sig
def signatures(_signatures: List) -> Signatures:
check_list(_signatures)
keyids: Signatures = {}
while len(_signatures) > 0:
# NOTE: pop in order of appearance, so that we preserve order of key IDs.
_signature = _signatures.pop(0)
keyid, sig = signature(_signature)
sigs = keyids.setdefault(keyid, set())
sigs.add(sig)
return keyids
class JSONParser(Parser):
@classmethod
def parse(cls, d: Json) -> Metadata:
"""This method is used to try to parse any JSON dictionary containing TUF metadata.
It destructively reads the dictionary in reverse order of canonical sorting.
If the original dictionary must be preserved, be sure to pass in a copy.
We assume that keys are sorted in both input and output.
We have always output keys in this order.
In Python >= 3.7, this order is preserved in input thanks to ordered dict.
It does NOT verify signatures. Be sure to verify signatures after parsing."""
check_dict(d)
k, _signed = d.popitem()
check_key(k, "signed")
# NOTE: Before we destroy the signed object, build its canonical representation.
_canonical = canonical(_signed)
_signed = signed(_signed)
k, _signatures = d.popitem()
check_key(k, "signatures")
_signatures = signatures(_signatures)
check_empty(d)
return Metadata(_canonical, _signatures, _signed)