forked from theupdateframework/python-tuf
/
formats.py
executable file
·1009 lines (775 loc) · 32.5 KB
/
formats.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright 2012 - 2017, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""
<Program Name>
formats.py
<Author>
Geremy Condra
Vladimir Diaz <vladimir.v.diaz@gmail.com>
<Started>
Refactored April 30, 2012. -vladimir.v.diaz
<Copyright>
See LICENSE-MIT OR LICENSE for licensing information.
<Purpose>
A central location for all format-related checking of TUF objects.
Some crypto-related formats may also be defined in securesystemslib.
Note: 'formats.py' depends heavily on 'schema.py', so the 'schema.py'
module should be read and understood before tackling this module.
'formats.py' can be broken down into two sections. (1) Schemas and object
matching. (2) Functions that help produce or verify TUF objects.
The first section deals with schemas and object matching based on format.
There are two ways of checking the format of objects. The first method
raises a 'securesystemslib.exceptions.FormatError' exception if the match
fails and the other returns a Boolean result.
tuf.formats.<SCHEMA>.check_match(object)
tuf.formats.<SCHEMA>.matches(object)
Example:
rsa_key = {'keytype': 'rsa'
'keyid': 34892fc465ac76bc3232fab
'keyval': {'public': 'public_key',
'private': 'private_key'}
securesystemslib.formats.RSAKEY_SCHEMA.check_match(rsa_key)
securesystemslib.formats.RSAKEY_SCHEMA.matches(rsa_key)
In this example, if a dict key or dict value is missing or incorrect,
the match fails. There are numerous variations of object checking
provided by 'formats.py' and 'schema.py'.
The second section contains miscellaneous functions related to the format of
TUF objects.
Example:
signable_object = make_signable(unsigned_object)
"""
import binascii
import calendar
import datetime
import time
import copy
from securesystemslib import exceptions as sslib_exceptions
from securesystemslib import formats as sslib_formats
from securesystemslib import schema as SCHEMA
import tuf
from tuf import exceptions
# As per TUF spec 1.0.0 the spec version field must follow the Semantic
# Versioning 2.0.0 (semver) format. The regex pattern is provided by semver.
# https://semver.org/spec/v2.0.0.html#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
SEMVER_2_0_0_SCHEMA = SCHEMA.RegularExpression(
r'(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)'
r'(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)'
r'(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?'
r'(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?'
)
SPECIFICATION_VERSION_SCHEMA = SCHEMA.OneOf([
# However, temporarily allow "1.0" for backwards-compatibility in tuf-0.12.PATCH.
SCHEMA.String("1.0"),
SEMVER_2_0_0_SCHEMA
])
# A datetime in 'YYYY-MM-DDTHH:MM:SSZ' ISO 8601 format. The "Z" zone designator
# for the zero UTC offset is always used (i.e., a numerical offset is not
# supported.) Example: '2015-10-21T13:20:00Z'. Note: This is a simple format
# check, and an ISO8601 string should be fully verified when it is parsed.
ISO8601_DATETIME_SCHEMA = SCHEMA.RegularExpression(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z')
# An integer representing the numbered version of a metadata file.
# Must be 1, or greater.
METADATAVERSION_SCHEMA = SCHEMA.Integer(lo=0)
# A relative file path (e.g., 'metadata/root/').
RELPATH_SCHEMA = SCHEMA.AnyString()
RELPATHS_SCHEMA = SCHEMA.ListOf(RELPATH_SCHEMA)
VERSIONINFO_SCHEMA = SCHEMA.Object(
object_name = 'VERSIONINFO_SCHEMA',
version = METADATAVERSION_SCHEMA)
# A string representing a role's name.
ROLENAME_SCHEMA = SCHEMA.AnyString()
# A role's threshold value (i.e., the minimum number
# of signatures required to sign a metadata file).
# Must be 1 and greater.
THRESHOLD_SCHEMA = SCHEMA.Integer(lo=1)
# A hexadecimal value in '23432df87ab..' format.
HEX_SCHEMA = SCHEMA.RegularExpression(r'[a-fA-F0-9]+')
# A path hash prefix is a hexadecimal string.
PATH_HASH_PREFIX_SCHEMA = HEX_SCHEMA
# A list of path hash prefixes.
PATH_HASH_PREFIXES_SCHEMA = SCHEMA.ListOf(PATH_HASH_PREFIX_SCHEMA)
# Role object in {'keyids': [keydids..], 'name': 'ABC', 'threshold': 1,
# 'paths':[filepaths..]} format.
# TODO: This is not a role. In further #660-related PRs, fix it, similar to
# the way I did in Uptane's TUF fork.
ROLE_SCHEMA = SCHEMA.Object(
object_name = 'ROLE_SCHEMA',
name = SCHEMA.Optional(ROLENAME_SCHEMA),
keyids = sslib_formats.KEYIDS_SCHEMA,
threshold = THRESHOLD_SCHEMA,
terminating = SCHEMA.Optional(sslib_formats.BOOLEAN_SCHEMA),
paths = SCHEMA.Optional(RELPATHS_SCHEMA),
path_hash_prefixes = SCHEMA.Optional(PATH_HASH_PREFIXES_SCHEMA))
# A dict of roles where the dict keys are role names and the dict values holding
# the role data/information.
ROLEDICT_SCHEMA = SCHEMA.DictOf(
key_schema = ROLENAME_SCHEMA,
value_schema = ROLE_SCHEMA)
# A dictionary of ROLEDICT, where dictionary keys can be repository names, and
# dictionary values containing information for each role available on the
# repository (corresponding to the repository belonging to named repository in
# the dictionary key)
ROLEDICTDB_SCHEMA = SCHEMA.DictOf(
key_schema = sslib_formats.NAME_SCHEMA,
value_schema = ROLEDICT_SCHEMA)
# Command argument list, as used by the CLI tool.
# Example: {'keytype': ed25519, 'expires': 365,}
COMMAND_SCHEMA = SCHEMA.DictOf(
key_schema = sslib_formats.NAME_SCHEMA,
value_schema = SCHEMA.Any())
# A dictionary holding version information.
VERSION_SCHEMA = SCHEMA.Object(
object_name = 'VERSION_SCHEMA',
major = SCHEMA.Integer(lo=0),
minor = SCHEMA.Integer(lo=0),
fix = SCHEMA.Integer(lo=0))
# A value that is either True or False, on or off, etc.
BOOLEAN_SCHEMA = SCHEMA.Boolean()
# A hexadecimal value in '23432df87ab..' format.
HASH_SCHEMA = SCHEMA.RegularExpression(r'[a-fA-F0-9]+')
# A key identifier (e.g., a hexadecimal value identifying an RSA key).
KEYID_SCHEMA = HASH_SCHEMA
# A list of KEYID_SCHEMA.
KEYIDS_SCHEMA = SCHEMA.ListOf(KEYID_SCHEMA)
# The actual values of a key, as opposed to meta data such as a key type and
# key identifier ('rsa', 233df889cb). For RSA keys, the key value is a pair of
# public and private keys in PEM Format stored as strings.
KEYVAL_SCHEMA = SCHEMA.Object(
object_name = 'KEYVAL_SCHEMA',
public = SCHEMA.AnyString(),
private = SCHEMA.Optional(SCHEMA.AnyString()))
# A generic TUF key. All TUF keys should be saved to metadata files in this
# format.
KEY_SCHEMA = SCHEMA.Object(
object_name = 'KEY_SCHEMA',
keytype = SCHEMA.AnyString(),
keyval = KEYVAL_SCHEMA,
expires = SCHEMA.Optional(ISO8601_DATETIME_SCHEMA))
# A dict where the dict keys hold a keyid and the dict values a key object.
KEYDICT_SCHEMA = SCHEMA.DictOf(
key_schema = KEYID_SCHEMA,
value_schema = KEY_SCHEMA)
# The format used by the key database to store keys. The dict keys hold a key
# identifier and the dict values any object. The key database should store
# key objects in the values (e.g., 'RSAKEY_SCHEMA', 'DSAKEY_SCHEMA').
KEYDB_SCHEMA = SCHEMA.DictOf(
key_schema = KEYID_SCHEMA,
value_schema = SCHEMA.Any())
# A schema holding the result of checking the signatures of a particular
# 'SIGNABLE_SCHEMA' role.
# For example, how many of the signatures for the 'Target' role are
# valid? This SCHEMA holds this information. See 'sig.py' for
# more information.
SIGNATURESTATUS_SCHEMA = SCHEMA.Object(
object_name = 'SIGNATURESTATUS_SCHEMA',
threshold = SCHEMA.Integer(),
good_sigs = KEYIDS_SCHEMA,
bad_sigs = KEYIDS_SCHEMA,
unknown_sigs = KEYIDS_SCHEMA,
untrusted_sigs = KEYIDS_SCHEMA)
# An integer representing length. Must be 0, or greater.
LENGTH_SCHEMA = SCHEMA.Integer(lo=0)
# A dict in {'sha256': '23432df87ab..', 'sha512': '34324abc34df..', ...} format.
HASHDICT_SCHEMA = SCHEMA.DictOf(
key_schema = SCHEMA.AnyString(),
value_schema = HASH_SCHEMA)
# Information about target files, like file length and file hash(es). This
# schema allows the storage of multiple hashes for the same file (e.g., sha256
# and sha512 may be computed for the same file and stored).
TARGETS_FILEINFO_SCHEMA = SCHEMA.Object(
object_name = 'TARGETS_FILEINFO_SCHEMA',
length = LENGTH_SCHEMA,
hashes = HASHDICT_SCHEMA,
custom = SCHEMA.Optional(SCHEMA.Object()))
# Information about snapshot and timestamp files. This schema allows for optional
# length and hashes, but version is mandatory.
METADATA_FILEINFO_SCHEMA = SCHEMA.Object(
object_name = 'METADATA_FILEINFO_SCHEMA',
length = SCHEMA.Optional(LENGTH_SCHEMA),
hashes = SCHEMA.Optional(HASHDICT_SCHEMA),
version = METADATAVERSION_SCHEMA)
# A dict holding the version or file information for a particular metadata
# role. The dict keys hold the relative file paths, and the dict values the
# corresponding version numbers and/or file information.
FILEINFODICT_SCHEMA = SCHEMA.DictOf(
key_schema = RELPATH_SCHEMA,
value_schema = SCHEMA.OneOf([VERSIONINFO_SCHEMA,
METADATA_FILEINFO_SCHEMA]))
# A dict holding the information for a particular target / file. The dict keys
# hold the relative file paths, and the dict values the corresponding file
# information.
FILEDICT_SCHEMA = SCHEMA.DictOf(
key_schema = RELPATH_SCHEMA,
value_schema = TARGETS_FILEINFO_SCHEMA)
# A dict holding a target info.
TARGETINFO_SCHEMA = SCHEMA.Object(
object_name = 'TARGETINFO_SCHEMA',
filepath = RELPATH_SCHEMA,
fileinfo = TARGETS_FILEINFO_SCHEMA)
# A list of TARGETINFO_SCHEMA.
TARGETINFOS_SCHEMA = SCHEMA.ListOf(TARGETINFO_SCHEMA)
# A string representing a named oject.
NAME_SCHEMA = SCHEMA.AnyString()
# A dict of repository names to mirrors.
REPO_NAMES_TO_MIRRORS_SCHEMA = SCHEMA.DictOf(
key_schema = NAME_SCHEMA,
value_schema = SCHEMA.ListOf(sslib_formats.URL_SCHEMA))
# An object containing the map file's "mapping" attribute.
MAPPING_SCHEMA = SCHEMA.ListOf(SCHEMA.Object(
paths = RELPATHS_SCHEMA,
repositories = SCHEMA.ListOf(NAME_SCHEMA),
terminating = BOOLEAN_SCHEMA,
threshold = THRESHOLD_SCHEMA))
# A dict containing the map file (named 'map.json', by default). The format of
# the map file is covered in TAP 4: Multiple repository consensus on entrusted
# targets.
MAPFILE_SCHEMA = SCHEMA.Object(
repositories = REPO_NAMES_TO_MIRRORS_SCHEMA,
mapping = MAPPING_SCHEMA)
# Like ROLEDICT_SCHEMA, except that ROLE_SCHEMA instances are stored in order.
ROLELIST_SCHEMA = SCHEMA.ListOf(ROLE_SCHEMA)
# The delegated roles of a Targets role (a parent).
DELEGATIONS_SCHEMA = SCHEMA.Object(
keys = KEYDICT_SCHEMA,
roles = ROLELIST_SCHEMA)
# The number of hashed bins, or the number of delegated roles. See
# delegate_hashed_bins() in 'repository_tool.py' for an example. Note:
# Tools may require further restrictions on the number of bins, such
# as requiring them to be a power of 2.
NUMBINS_SCHEMA = SCHEMA.Integer(lo=1)
# The fileinfo format of targets specified in the repository and
# developer tools. The fields match that of TARGETS_FILEINFO_SCHEMA, only all
# fields are optional.
CUSTOM_SCHEMA = SCHEMA.DictOf(
key_schema = SCHEMA.AnyString(),
value_schema = SCHEMA.Any()
)
LOOSE_TARGETS_FILEINFO_SCHEMA = SCHEMA.Object(
object_name = "LOOSE_TARGETS_FILEINFO_SCHEMA",
length = SCHEMA.Optional(LENGTH_SCHEMA),
hashes = SCHEMA.Optional(HASHDICT_SCHEMA),
version = SCHEMA.Optional(METADATAVERSION_SCHEMA),
custom = SCHEMA.Optional(SCHEMA.Object())
)
PATH_FILEINFO_SCHEMA = SCHEMA.DictOf(
key_schema = RELPATH_SCHEMA,
value_schema = LOOSE_TARGETS_FILEINFO_SCHEMA)
# TUF roledb
ROLEDB_SCHEMA = SCHEMA.Object(
object_name = 'ROLEDB_SCHEMA',
keyids = SCHEMA.Optional(KEYIDS_SCHEMA),
signing_keyids = SCHEMA.Optional(KEYIDS_SCHEMA),
previous_keyids = SCHEMA.Optional(KEYIDS_SCHEMA),
threshold = SCHEMA.Optional(THRESHOLD_SCHEMA),
previous_threshold = SCHEMA.Optional(THRESHOLD_SCHEMA),
version = SCHEMA.Optional(METADATAVERSION_SCHEMA),
expires = SCHEMA.Optional(ISO8601_DATETIME_SCHEMA),
signatures = SCHEMA.Optional(sslib_formats.SIGNATURES_SCHEMA),
paths = SCHEMA.Optional(SCHEMA.OneOf([RELPATHS_SCHEMA, PATH_FILEINFO_SCHEMA])),
path_hash_prefixes = SCHEMA.Optional(PATH_HASH_PREFIXES_SCHEMA),
delegations = SCHEMA.Optional(DELEGATIONS_SCHEMA),
partial_loaded = SCHEMA.Optional(BOOLEAN_SCHEMA))
# A signable object. Holds the signing role and its associated signatures.
SIGNABLE_SCHEMA = SCHEMA.Object(
object_name = 'SIGNABLE_SCHEMA',
signed = SCHEMA.Any(),
signatures = SCHEMA.ListOf(sslib_formats.SIGNATURE_SCHEMA))
# Root role: indicates root keys and top-level roles.
ROOT_SCHEMA = SCHEMA.Object(
object_name = 'ROOT_SCHEMA',
_type = SCHEMA.String('root'),
spec_version = SPECIFICATION_VERSION_SCHEMA,
version = METADATAVERSION_SCHEMA,
consistent_snapshot = BOOLEAN_SCHEMA,
expires = ISO8601_DATETIME_SCHEMA,
keys = KEYDICT_SCHEMA,
roles = ROLEDICT_SCHEMA)
# Targets role: Indicates targets and delegates target paths to other roles.
TARGETS_SCHEMA = SCHEMA.Object(
object_name = 'TARGETS_SCHEMA',
_type = SCHEMA.String('targets'),
spec_version = SPECIFICATION_VERSION_SCHEMA,
version = METADATAVERSION_SCHEMA,
expires = ISO8601_DATETIME_SCHEMA,
targets = FILEDICT_SCHEMA,
delegations = SCHEMA.Optional(DELEGATIONS_SCHEMA))
# Snapshot role: indicates the latest versions of all metadata (except
# timestamp).
SNAPSHOT_SCHEMA = SCHEMA.Object(
object_name = 'SNAPSHOT_SCHEMA',
_type = SCHEMA.String('snapshot'),
version = METADATAVERSION_SCHEMA,
expires = sslib_formats.ISO8601_DATETIME_SCHEMA,
spec_version = SPECIFICATION_VERSION_SCHEMA,
meta = FILEINFODICT_SCHEMA)
# Timestamp role: indicates the latest version of the snapshot file.
TIMESTAMP_SCHEMA = SCHEMA.Object(
object_name = 'TIMESTAMP_SCHEMA',
_type = SCHEMA.String('timestamp'),
spec_version = SPECIFICATION_VERSION_SCHEMA,
version = METADATAVERSION_SCHEMA,
expires = sslib_formats.ISO8601_DATETIME_SCHEMA,
meta = FILEINFODICT_SCHEMA)
# project.cfg file: stores information about the project in a json dictionary
PROJECT_CFG_SCHEMA = SCHEMA.Object(
object_name = 'PROJECT_CFG_SCHEMA',
project_name = SCHEMA.AnyString(),
layout_type = SCHEMA.OneOf([SCHEMA.String('repo-like'), SCHEMA.String('flat')]),
targets_location = sslib_formats.PATH_SCHEMA,
metadata_location = sslib_formats.PATH_SCHEMA,
prefix = sslib_formats.PATH_SCHEMA,
public_keys = sslib_formats.KEYDICT_SCHEMA,
threshold = SCHEMA.Integer(lo = 0, hi = 2)
)
# A schema containing information a repository mirror may require,
# such as a url, the path of the directory metadata files, etc.
MIRROR_SCHEMA = SCHEMA.Object(
object_name = 'MIRROR_SCHEMA',
url_prefix = sslib_formats.URL_SCHEMA,
metadata_path = SCHEMA.Optional(RELPATH_SCHEMA),
targets_path = SCHEMA.Optional(RELPATH_SCHEMA),
confined_target_dirs = SCHEMA.Optional(RELPATHS_SCHEMA),
custom = SCHEMA.Optional(SCHEMA.Object()))
# A dictionary of mirrors where the dict keys hold the mirror's name and
# and the dict values the mirror's data (i.e., 'MIRROR_SCHEMA').
# The repository class of 'updater.py' accepts dictionaries
# of this type provided by the TUF client.
MIRRORDICT_SCHEMA = SCHEMA.DictOf(
key_schema = SCHEMA.AnyString(),
value_schema = MIRROR_SCHEMA)
# A Mirrorlist: indicates all the live mirrors, and what documents they
# serve.
MIRRORLIST_SCHEMA = SCHEMA.Object(
object_name = 'MIRRORLIST_SCHEMA',
_type = SCHEMA.String('mirrors'),
version = METADATAVERSION_SCHEMA,
expires = sslib_formats.ISO8601_DATETIME_SCHEMA,
mirrors = SCHEMA.ListOf(MIRROR_SCHEMA))
# Any of the role schemas (e.g., TIMESTAMP_SCHEMA, SNAPSHOT_SCHEMA, etc.)
ANYROLE_SCHEMA = SCHEMA.OneOf([ROOT_SCHEMA, TARGETS_SCHEMA, SNAPSHOT_SCHEMA,
TIMESTAMP_SCHEMA, MIRROR_SCHEMA])
# The format of the resulting "scp config dict" after extraction from the
# push configuration file (i.e., push.cfg). In the case of a config file
# utilizing the scp transfer module, it must contain the 'general' and 'scp'
# sections, where 'general' must contain a 'transfer_module' and
# 'metadata_path' entry, and 'scp' the 'host', 'user', 'identity_file', and
# 'remote_directory' entries.
SCPCONFIG_SCHEMA = SCHEMA.Object(
object_name = 'SCPCONFIG_SCHEMA',
general = SCHEMA.Object(
object_name = '[general]',
transfer_module = SCHEMA.String('scp'),
metadata_path = sslib_formats.PATH_SCHEMA,
targets_directory = sslib_formats.PATH_SCHEMA),
scp=SCHEMA.Object(
object_name = '[scp]',
host = sslib_formats.URL_SCHEMA,
user = sslib_formats.NAME_SCHEMA,
identity_file = sslib_formats.PATH_SCHEMA,
remote_directory = sslib_formats.PATH_SCHEMA))
# The format of the resulting "receive config dict" after extraction from the
# receive configuration file (i.e., receive.cfg). The receive config file
# must contain a 'general' section, and this section the 'pushroots',
# 'repository_directory', 'metadata_directory', 'targets_directory', and
# 'backup_directory' entries.
RECEIVECONFIG_SCHEMA = SCHEMA.Object(
object_name = 'RECEIVECONFIG_SCHEMA', general=SCHEMA.Object(
object_name = '[general]',
pushroots = SCHEMA.ListOf(sslib_formats.PATH_SCHEMA),
repository_directory = sslib_formats.PATH_SCHEMA,
metadata_directory = sslib_formats.PATH_SCHEMA,
targets_directory = sslib_formats.PATH_SCHEMA,
backup_directory = sslib_formats.PATH_SCHEMA))
def make_signable(role_schema):
"""
<Purpose>
Return the role metadata 'role_schema' in 'SIGNABLE_SCHEMA' format.
'role_schema' is added to the 'signed' key, and an empty list
initialized to the 'signatures' key. The caller adds signatures
to this second field.
Note: check_signable_object_format() should be called after
make_signable() and signatures added to ensure the final
signable object has a valid format (i.e., a signable containing
a supported role metadata).
<Arguments>
role_schema:
A role schema dict (e.g., 'ROOT_SCHEMA', 'SNAPSHOT_SCHEMA').
<Exceptions>
None.
<Side Effects>
None.
<Returns>
A dict in 'SIGNABLE_SCHEMA' format.
"""
if not isinstance(role_schema, dict) or 'signed' not in role_schema:
return { 'signed' : role_schema, 'signatures' : [] }
else:
return role_schema
def build_dict_conforming_to_schema(schema, **kwargs):
"""
<Purpose>
Given a schema.Object object (for example, TIMESTAMP_SCHEMA from this
module) and a set of keyword arguments, create a dictionary that conforms
to the given schema, using the keyword arguments to define the elements of
the new dict.
Checks the result to make sure that it conforms to the given schema, raising
an error if not.
<Arguments>
schema
A schema.Object, like TIMESTAMP_SCHEMA, TARGETS_FILEINFO_SCHEMA,
securesystemslib.formats.SIGNATURE_SCHEMA, etc.
**kwargs
A keyword argument for each element of the schema. Optional arguments
may be included or skipped, but all required arguments must be included.
For example, for TIMESTAMP_SCHEMA, a call might look like:
build_dict_conforming_to_schema(
TIMESTAMP_SCHEMA,
_type='timestamp',
spec_version='1.0.0',
version=1,
expires='2020-01-01T00:00:00Z',
meta={...})
Some arguments will be filled in if excluded: _type, spec_version
<Returns>
A dictionary conforming to the given schema. Adds certain required fields
if they are missing and can be deduced from the schema. The data returned
is a deep copy.
<Exceptions>
securesystemslib.exceptions.FormatError
if the provided data does not match the schema when assembled.
<Side Effects>
None. In particular, the provided values are not modified, and the
returned dictionary does not include references to them.
"""
# Check the schema argument type (must provide check_match and _required).
if not isinstance(schema, SCHEMA.Object):
raise ValueError(
'The first argument must be a schema.Object instance, but is not. '
'Given schema: ' + repr(schema))
# Make a copy of the provided fields so that the caller's provided values
# do not change when the returned values are changed.
dictionary = copy.deepcopy(kwargs)
# Automatically provide certain schema properties if they are not already
# provided and are required in objects of class <schema>.
# This includes:
# _type: <securesystemslib.schema.String object>
# spec_version: SPECIFICATION_VERSION_SCHEMA
#
# (Please note that _required is slightly misleading, as it includes both
# required and optional elements. It should probably be called _components.)
#
for key, element_type in schema._required: #pylint: disable=protected-access
if key in dictionary:
# If the field has been provided, proceed normally.
continue
elif isinstance(element_type, SCHEMA.Optional):
# If the field has NOT been provided but IS optional, proceed without it.
continue
else:
# If the field has not been provided and is required, check to see if
# the field is one of the fields we automatically fill.
# Currently, the list is limited to ['_type', 'spec_version'].
if key == '_type' and isinstance(element_type, SCHEMA.String):
# A SCHEMA.String stores its expected value in _string, so use that.
dictionary[key] = element_type._string #pylint: disable=protected-access
elif (key == 'spec_version' and
element_type == SPECIFICATION_VERSION_SCHEMA):
# If not provided, use the specification version in tuf/__init__.py
dictionary[key] = tuf.SPECIFICATION_VERSION
# If what we produce does not match the provided schema, raise a FormatError.
schema.check_match(dictionary)
return dictionary
# A dict holding the recognized schemas for the top-level roles.
SCHEMAS_BY_TYPE = {
'root' : ROOT_SCHEMA,
'targets' : TARGETS_SCHEMA,
'snapshot' : SNAPSHOT_SCHEMA,
'timestamp' : TIMESTAMP_SCHEMA,
'mirrors' : MIRRORLIST_SCHEMA}
def expiry_string_to_datetime(expires):
"""
<Purpose>
Convert an expiry string to a datetime object.
<Arguments>
expires:
The expiry date-time string in the ISO8601 format that is defined
in securesystemslib.ISO8601_DATETIME_SCHEMA. E.g. '2038-01-19T03:14:08Z'
<Exceptions>
securesystemslib.exceptions.FormatError, if 'expires' cannot be
parsed correctly.
<Side Effects>
None.
<Returns>
A datetime object representing the expiry time.
"""
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
sslib_formats.ISO8601_DATETIME_SCHEMA.check_match(expires)
try:
return datetime.datetime.strptime(expires, "%Y-%m-%dT%H:%M:%SZ")
except ValueError as error:
raise sslib_exceptions.FormatError(
'Failed to parse ' + repr(expires) + ' as an expiry time') from error
def datetime_to_unix_timestamp(datetime_object):
"""
<Purpose>
Convert 'datetime_object' (in datetime.datetime()) format) to a Unix/POSIX
timestamp. For example, Python's time.time() returns a Unix timestamp, and
includes the number of microseconds. 'datetime_object' is converted to UTC.
>>> datetime_object = datetime.datetime(1985, 10, 26, 1, 22)
>>> timestamp = datetime_to_unix_timestamp(datetime_object)
>>> timestamp
499137720
<Arguments>
datetime_object:
The datetime.datetime() object to convert to a Unix timestamp.
<Exceptions>
securesystemslib.exceptions.FormatError, if 'datetime_object' is not a
datetime.datetime() object.
<Side Effects>
None.
<Returns>
A unix (posix) timestamp (e.g., 499137660).
"""
# Is 'datetime_object' a datetime.datetime() object?
# Raise 'securesystemslib.exceptions.FormatError' if not.
if not isinstance(datetime_object, datetime.datetime):
message = repr(datetime_object) + ' is not a datetime.datetime() object.'
raise sslib_exceptions.FormatError(message)
unix_timestamp = calendar.timegm(datetime_object.timetuple())
return unix_timestamp
def unix_timestamp_to_datetime(unix_timestamp):
"""
<Purpose>
Convert 'unix_timestamp' (i.e., POSIX time, in UNIX_TIMESTAMP_SCHEMA format)
to a datetime.datetime() object. 'unix_timestamp' is the number of seconds
since the epoch (January 1, 1970.)
>>> datetime_object = unix_timestamp_to_datetime(1445455680)
>>> datetime_object
datetime.datetime(2015, 10, 21, 19, 28)
<Arguments>
unix_timestamp:
An integer representing the time (e.g., 1445455680). Conformant to
'securesystemslib.formats.UNIX_TIMESTAMP_SCHEMA'.
<Exceptions>
securesystemslib.exceptions.FormatError, if 'unix_timestamp' is improperly
formatted.
<Side Effects>
None.
<Returns>
A datetime.datetime() object corresponding to 'unix_timestamp'.
"""
# Is 'unix_timestamp' properly formatted?
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
sslib_formats.UNIX_TIMESTAMP_SCHEMA.check_match(unix_timestamp)
# Convert 'unix_timestamp' to a 'time.struct_time', in UTC. The Daylight
# Savings Time (DST) flag is set to zero. datetime.fromtimestamp() is not
# used because it returns a local datetime.
struct_time = time.gmtime(unix_timestamp)
# Extract the (year, month, day, hour, minutes, seconds) arguments for the
# datetime object to be returned.
datetime_object = datetime.datetime(*struct_time[:6])
return datetime_object
def format_base64(data):
"""
<Purpose>
Return the base64 encoding of 'data' with whitespace and '=' signs omitted.
<Arguments>
data:
Binary or buffer of data to convert.
<Exceptions>
securesystemslib.exceptions.FormatError, if the base64 encoding fails or the
argument is invalid.
<Side Effects>
None.
<Returns>
A base64-encoded string.
"""
try:
return binascii.b2a_base64(data).decode('utf-8').rstrip('=\n ')
except (TypeError, binascii.Error) as e:
raise sslib_exceptions.FormatError('Invalid base64'
' encoding: ' + str(e))
def parse_base64(base64_string):
"""
<Purpose>
Parse a base64 encoding with whitespace and '=' signs omitted.
<Arguments>
base64_string:
A string holding a base64 value.
<Exceptions>
securesystemslib.exceptions.FormatError, if 'base64_string' cannot be parsed
due to an invalid base64 encoding.
<Side Effects>
None.
<Returns>
A byte string representing the parsed based64 encoding of
'base64_string'.
"""
if not isinstance(base64_string, str):
message = 'Invalid argument: '+repr(base64_string)
raise sslib_exceptions.FormatError(message)
extra = len(base64_string) % 4
if extra:
padding = '=' * (4 - extra)
base64_string = base64_string + padding
try:
return binascii.a2b_base64(base64_string.encode('utf-8'))
except (TypeError, binascii.Error) as e:
raise sslib_exceptions.FormatError('Invalid base64'
' encoding: ' + str(e))
def make_targets_fileinfo(length, hashes, custom=None):
"""
<Purpose>
Create a dictionary conformant to 'TARGETS_FILEINFO_SCHEMA'.
This dict describes a target file.
<Arguments>
length:
An integer representing the size of the file.
hashes:
A dict of hashes in 'HASHDICT_SCHEMA' format, which has the form:
{'sha256': 123df8a9b12, 'sha512': 324324dfc121, ...}
custom:
An optional object providing additional information about the file.
<Exceptions>
securesystemslib.exceptions.FormatError, if the 'TARGETS_FILEINFO_SCHEMA' to be
returned does not have the correct format.
<Returns>
A dictionary conformant to 'TARGETS_FILEINFO_SCHEMA', representing the file
information of a target file.
"""
fileinfo = {'length' : length, 'hashes' : hashes}
if custom is not None:
fileinfo['custom'] = custom
# Raise 'securesystemslib.exceptions.FormatError' if the check fails.
TARGETS_FILEINFO_SCHEMA.check_match(fileinfo)
return fileinfo
def make_metadata_fileinfo(version, length=None, hashes=None):
"""
<Purpose>
Create a dictionary conformant to 'METADATA_FILEINFO_SCHEMA'.
This dict describes one of the metadata files used for timestamp and
snapshot roles.
<Arguments>
version:
An integer representing the version of the file.
length:
An optional integer representing the size of the file.
hashes:
An optional dict of hashes in 'HASHDICT_SCHEMA' format, which has the form:
{'sha256': 123df8a9b12, 'sha512': 324324dfc121, ...}
<Exceptions>
securesystemslib.exceptions.FormatError, if the 'METADATA_FILEINFO_SCHEMA' to be
returned does not have the correct format.
<Returns>
A dictionary conformant to 'METADATA_FILEINFO_SCHEMA', representing the file
information of a metadata file.
"""
fileinfo = {'version' : version}
if length:
fileinfo['length'] = length
if hashes:
fileinfo['hashes'] = hashes
# Raise 'securesystemslib.exceptions.FormatError' if the check fails.
METADATA_FILEINFO_SCHEMA.check_match(fileinfo)
return fileinfo
def make_versioninfo(version_number):
"""
<Purpose>
Create a dictionary conformant to 'VERSIONINFO_SCHEMA'. This dict
describes both metadata and target files.
<Arguments>
version_number:
An integer representing the version of a particular metadata role.
The dictionary returned by this function is expected to be included
in Snapshot metadata.
<Exceptions>
securesystemslib.exceptions.FormatError, if the dict to be returned does not
have the correct format (i.e., VERSIONINFO_SCHEMA).
<Side Effects>
None.
<Returns>
A dictionary conformant to 'VERSIONINFO_SCHEMA', containing the version
information of a metadata role.
"""
versioninfo = {'version': version_number}
# Raise 'securesystemslib.exceptions.FormatError' if 'versioninfo' is
# improperly formatted.
VERSIONINFO_SCHEMA.check_match(versioninfo)
return versioninfo
def expected_meta_rolename(meta_rolename):
"""
<Purpose>
Ensure 'meta_rolename' is properly formatted.
'targets' is returned as 'Targets'.
'targets role1' is returned as 'Targets Role1'.
The words in the string (i.e., separated by whitespace)
are capitalized.
<Arguments>
meta_rolename:
A string representing the rolename.
E.g., 'root', 'targets'.
<Exceptions>
securesystemslib.exceptions.FormatError, if 'meta_rolename' is improperly
formatted.
<Side Effects>
None.
<Returns>
A string (e.g., 'Root', 'Targets').
"""
# Does 'meta_rolename' have the correct type?
# This check ensures 'meta_rolename' conforms to
# 'securesystemslib.formats.NAME_SCHEMA'.
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
sslib_formats.NAME_SCHEMA.check_match(meta_rolename)
return meta_rolename.lower()
def check_signable_object_format(signable):
"""
<Purpose>
Ensure 'signable' is properly formatted, conformant to
'SIGNABLE_SCHEMA'. Return the signing role on
success. Note: The 'signed' field of a 'SIGNABLE_SCHEMA' is checked
against securesystemslib.schema.Any(). The 'signed' field, however, should
actually hold one of the supported role schemas (e.g., 'ROOT_SCHEMA',
'TARGETS_SCHEMA'). The role schemas all differ in their format, so this
function determines exactly which schema is listed in the 'signed' field.
<Arguments>
signable:
The signable object compared against 'SIGNABLE.SCHEMA'.
<Exceptions>
securesystemslib.exceptions.FormatError, if 'signable' does not have the
correct format.
tuf.exceptions.UnsignedMetadataError, if 'signable' does not have any
signatures
<Side Effects>
None.
<Returns>
A string representing the signing role (e.g., 'root', 'targets').
The role string is returned with characters all lower case.
"""
# Does 'signable' have the correct type?
# This check ensures 'signable' conforms to
# 'SIGNABLE_SCHEMA'.
SIGNABLE_SCHEMA.check_match(signable)
try:
role_type = signable['signed']['_type']
except (KeyError, TypeError) as error:
raise sslib_exceptions.FormatError('Untyped signable object.') from error
try:
schema = SCHEMAS_BY_TYPE[role_type]
except KeyError as error:
raise sslib_exceptions.FormatError('Unrecognized type '
+ repr(role_type)) from error
if not signable['signatures']:
raise exceptions.UnsignedMetadataError('Signable object of type ' +
repr(role_type) + ' has no signatures ', signable)
# 'securesystemslib.exceptions.FormatError' raised if 'signable' does not
# have a properly formatted role schema.
schema.check_match(signable['signed'])
return role_type.lower()