This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
uptane_tuf_server.py
411 lines (301 loc) · 11.9 KB
/
uptane_tuf_server.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
#
# ! THIS FILE IS OUTDATED
"""
<Program Name>
uptane_tuf_server.py
<Purpose>
Create, load, manipulate, and host Uptane's TUF repo.
Running this as a script creates a repo with name REPO_NAME at directory
REPO_PATH.
In order to load a repository with name REPO_NAME at path ROOT_PATH, import
this module and run load_repo().
Sample use:
import uptane_tuf_server as uts
uts.clean_slate()
uts.host_repo()
The file is dividied into REPOSITORY CORE FUNCTIONALITY and ATTACK CODE.
"""
import tuf
import tuf.repository_tool as repotool
import os # for chdir
import shutil # for copying
import subprocess # for hosting
import time # for sleep
import sys # for python version
import datetime # for metadata expiration times
# Constants
ROOT_PATH = '/Users/s/w/uptane'
REPO_NAME = 'temp_tufrepo'
REPO_PATH = ROOT_PATH + '/' + REPO_NAME
METADATA_STAGED_PATH = REPO_PATH + '/metadata.staged'
METADATA_LIVE_PATH = REPO_PATH + '/metadata'
KEYS_DIR = REPO_PATH + '/keys/' # Online keys
#KEYS_ONLINE_DIR = REPO_PATH + '/keys/''
KEYS_OFFLINE_DIR = REPO_PATH + '/offline_keys/'
IMAGES_DIR = REPO_PATH + '/targets/images/'
SERVER_PORT = 33331
CLEAN_REPO_NAME = 'clean_tufrepo'
CLEAN_REPO_PATH = ROOT_PATH + '/' + CLEAN_REPO_NAME
CLEAN_METADATA_PATH = CLEAN_REPO_PATH + '/metadata'
CLEAN_KEYS_DIR = CLEAN_REPO_PATH + '/keys/'
CLEAN_KEYS_OFFLINE_DIR = CLEAN_REPO_PATH + '/offline_keys/'
CLEAN_IMAGES_DIR = CLEAN_REPO_PATH + '/targets/images/'
# Globals
repo = None
url = 'http://localhost:'+str(SERVER_PORT) + '/'
public_root_key = None
public_time_key = None
public_snap_key = None
public_targets_key = None
public_images_key = None
public_director_key = None
public_brakes_key = None
public_acme_key = None
public_cell_key = None
private_root_key = None
private_time_key = None
private_snap_key = None
private_targets_key = None
private_images_key = None
private_director_key = None
private_brakes_key = None
private_acme_key = None
private_cell_key = None
def host_repo():
"""
Loads the current repository at REPO_PATH and hosts it via http on port
SERVER_PORT for 10,000 seconds, ending early if any exception is raised.
Uses python's standard simple http server, and should be Python 2/3
compatible.
"""
global repo
global url
os.chdir(ROOT_PATH)
# Loading repositories in TUF 1.0 is currently broken.
# A fresh repository has to be created every time.
if repo is None:
repo = load_repo()
# Copy staging metadata to live metadata path, where it will be used as a TUF
# repo. Clear any old live metadata.
if os.path.exists(METADATA_LIVE_PATH):
shutil.rmtree(METADATA_LIVE_PATH)
shutil.copytree(METADATA_STAGED_PATH, METADATA_LIVE_PATH)
os.chdir(REPO_PATH)
# Python 2/3 compatibility.
command = []
if sys.version_info.major < 3:
command = ['python', '-m', 'SimpleHTTPServer', str(SERVER_PORT)]
else:
command = ['python', '-m', 'http.server', str(SERVER_PORT)]
server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
print('Server process started.')
print('Server process id: ' + str(server_process.pid))
print('Serving on port: ' + str(SERVER_PORT))
url = 'http://localhost:' + str(SERVER_PORT) + '/'
print('Repo URL is: ' + url)
# Host and wait.
try:
time.sleep(10000)
except:
print('Exception caught')
pass
finally:
if server_process.returncode is None:
print('Terminating server process ' + str(server_process.pid))
server_process.kill()
def clean_slate():
"""
Delete current repository at REPO_PATH and replace it with a fresh copy of
the clean original repository.
"""
if os.path.exists(REPO_PATH):
shutil.rmtree(REPO_PATH)
shutil.copytree(CLEAN_REPO_PATH, REPO_PATH)
# Unload any repository currently loaded.
global repo
repo = None
def load_repo():
"""
Loads the repo last written to REPO_PATH.
"""
global repo
os.chdir(ROOT_PATH)
repo = repotool.load_repository(REPO_NAME)
import_all_keys()
add_top_level_keys_to_repo()
add_delegated_keys_to_repo()
return repo
def create_new_repo():
"""
Creates a fresh TUF repo for uptane, with new keys for all roles, using the
role structure defined below, and with targets files copied from the clean
repo.
"""
global repo
repo = None
# Delete current repo if it exists.
if os.path.exists(REPO_PATH):
shutil.rmtree(REPO_PATH)
os.chdir(ROOT_PATH)
repo = repotool.create_new_repository(REPO_NAME)
# Create key pairs for all roles.
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'root', password="pw")
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'time', password="pw")
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'snap', password="pw")
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'targets', password="pw")
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'images', password="pw")
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'director', password="pw")
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'brakes', password="pw")
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'acme', password="pw")
# repotool.generate_and_write_rsa_keypair(KEYS_DIR + 'cell', password="pw")
# Move the offline private keys to the offline folder.
#shutil.move(KEYS_DIR + 'root', KEYS_OFFLINE_DIR + 'root')
#shutil.move(KEYS_DIR + 'targets', KEYS_OFFLINE_DIR + 'targets')
#shutil.move(KEYS_DIR + 'images', KEYS_OFFLINE_DIR + 'images')
#shutil.move(KEYS_DIR + 'brakes', KEYS_OFFLINE_DIR + 'brakes')
#shutil.move(KEYS_DIR + 'acme', KEYS_OFFLINE_DIR + 'acme')
#shutil.move(KEYS_DIR + 'cell', KEYS_OFFLINE_DIR + 'cell')
# Instead of creating new keys, just copy them from the clean repo.
shutil.copytree(CLEAN_KEYS_DIR, KEYS_DIR)
shutil.copytree(CLEAN_KEYS_OFFLINE_DIR, KEYS_OFFLINE_DIR)
import_all_keys()
add_top_level_keys_to_repo()
# Perform some delegations.
repo.targets.delegate('images', [public_images_key],
[], restricted_paths=[])
repo.targets.delegate('director', [public_director_key],
[], restricted_paths=[])
repo.targets('images').delegate('brakes', [public_brakes_key],
[], restricted_paths=[IMAGES_DIR + 'brakes/*'])
repo.targets('images').delegate('acme', [public_acme_key],
[], restricted_paths=[IMAGES_DIR + 'flobinator/acme/*'])
repo.targets('images').delegate('cell', [public_cell_key],
[], restricted_paths=[IMAGES_DIR + 'cellfw/*'])
# Perform the multi-role delegation giving Director + Images control of images.
restricted_paths = [IMAGES_DIR]
required_roles = ['images', 'director']
repo.targets.multi_role_delegate(restricted_paths, required_roles)
add_delegated_keys_to_repo()
# Now we add targets, first copying the target files themselves from the
# clean targets directory, then adding their file info to the repository.
if os.path.exists(IMAGES_DIR):
shutil.rmtree(IMAGES_DIR)
shutil.copytree(CLEAN_IMAGES_DIR, IMAGES_DIR)
time.sleep(0.1) # For the shutil copy....?
repo.targets('brakes').add_targets([
IMAGES_DIR + 'brakes/E859A50_9613.zip',
IMAGES_DIR + 'brakes/config/someconfig.cfg',
IMAGES_DIR + 'brakes/firmware.py',
IMAGES_DIR + 'brakes/firmware_legacy.py',
IMAGES_DIR + 'brakes/firmware_alternative.py'])
repo.targets('cell').add_targets([
IMAGES_DIR + 'cellfw/infotainment_adjacent_fw.zip'])
repo.targets('acme').add_targets([
IMAGES_DIR + 'flobinator/acme/1111111.txt',
IMAGES_DIR + 'flobinator/acme/firmware.py',
IMAGES_DIR + 'flobinator/acme/b20.zip'])
# Add two of those targets to the director role as well.
repo.targets('director').add_targets([
IMAGES_DIR + 'brakes/E859A50_9613.zip',
IMAGES_DIR + 'brakes/firmware.py'])
# For demo and github convenience (only!), have all metadata expire in a year.
expiry = datetime.datetime(2017, 8, 15, 0, 0, 0)
for role in [repo.timestamp, repo.snapshot, repo.root, repo.targets,
repo.targets('director'), repo.targets('images'),
repo.targets('cell'), repo.targets('brakes'),
repo.targets('acme')]:
role.expiration = expiry
# Save the repo's metadata to files.
repo.write()
return repo
def import_all_keys(online_only=False):
global public_root_key
global public_time_key
global public_snap_key
global public_targets_key
global public_images_key
global public_director_key
global public_brakes_key
global public_acme_key
global public_cell_key
global private_root_key
global private_time_key
global private_snap_key
global private_targets_key
global private_images_key
global private_director_key
global private_brakes_key
global private_acme_key
global private_cell_key
# Import public and private keys from the generated files.
public_root_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'root.pub')
public_time_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'time.pub')
public_snap_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'snap.pub')
public_targets_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'targets.pub')
private_time_key = repotool.import_rsa_privatekey_from_file(KEYS_DIR +
'time', password='pw')
private_snap_key = repotool.import_rsa_privatekey_from_file(KEYS_DIR +
'snap', password='pw')
if not online_only:
private_root_key = repotool.import_rsa_privatekey_from_file(
KEYS_OFFLINE_DIR + 'root', password='pw')
private_targets_key = repotool.import_rsa_privatekey_from_file(
KEYS_OFFLINE_DIR + 'targets', password='pw')
# Import delegated keys.
public_images_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'images.pub')
public_director_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'director.pub')
public_brakes_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'brakes.pub')
public_acme_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'acme.pub')
public_cell_key = repotool.import_rsa_publickey_from_file(KEYS_DIR +
'cell.pub')
private_director_key = repotool.import_rsa_privatekey_from_file(KEYS_DIR +
'director', password='pw')
if not online_only:
private_images_key = repotool.import_rsa_privatekey_from_file(
KEYS_OFFLINE_DIR + 'images', password='pw')
private_brakes_key = repotool.import_rsa_privatekey_from_file(
KEYS_OFFLINE_DIR + 'brakes', password='pw')
private_acme_key = repotool.import_rsa_privatekey_from_file(
KEYS_OFFLINE_DIR + 'acme', password='pw')
private_cell_key = repotool.import_rsa_privatekey_from_file(
KEYS_OFFLINE_DIR + 'cell', password='pw')
def add_top_level_keys_to_repo():
global repo
# Add public keys to repo.
repo.root.add_verification_key(public_root_key)
repo.timestamp.add_verification_key(public_time_key)
repo.snapshot.add_verification_key(public_snap_key)
repo.targets.add_verification_key(public_targets_key)
# Add private keys to repo.
repo.root.load_signing_key(private_root_key)
repo.timestamp.load_signing_key(private_time_key)
repo.snapshot.load_signing_key(private_snap_key)
repo.targets.load_signing_key(private_targets_key)
def add_delegated_keys_to_repo():
global repo
# The delegation itself already added the new roles' public keys. We still have
# to add their private keys.
repo.targets('images').load_signing_key(private_images_key)
repo.targets('director').load_signing_key(private_director_key)
repo.targets('brakes').load_signing_key(private_brakes_key)
repo.targets('acme').load_signing_key(private_acme_key)
repo.targets('cell').load_signing_key(private_cell_key)
# repo.targets('images')('brakes').load_signing_key(private_brakes_key)
# repo.targets('images')('acme').load_signing_key(private_acme_key)
# repo.targets('images')('cell').load_signing_key(private_cell_key)
def main():
# Create a new repository of name REPO_NAME at path ROOT_PATH.
global repo
os.chdir(ROOT_PATH)
clean_slate()
host_repo()
if __name__ == '__main__':
main()