-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_tools.py
330 lines (242 loc) · 7.63 KB
/
chain_tools.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
import base64
import json
import time
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.exceptions import InvalidSignature
def base64_string(bytes):
bytes_base64 = base64.b64encode(bytes)
string = bytes_base64.decode('utf-8')
return string
def log(text):
text_base = '{time} | {text}'
print(text_base.format(
time = time.strftime( "%H:%M:%S", time.localtime() ),
text = text,
))
def is_base64_string(input, **kwargs):
allow_none = kwargs.pop('allow_none', False)
if allow_none and input is None:
return True
try:
decoded = base64.b64decode(input)
encoded = base64.b64encode(decoded)
except:
return False
output = encoded.decode('utf-8')
return input == output
def pretty_log(object):
serialized_pretty = json.dumps(
object,
indent='\t',
)
log(serialized_pretty)
def clean_chainlink(untrusted_chainlink):
def devices_clean(untrusted_devices):
devices = {}
for device_key, device_name in untrusted_devices.items():
if is_base64_string(device_key):
devices[device_key] = device_name
else: return False
return devices
untrusted_devices = untrusted_chainlink['signed']['devices']
devices = devices_clean(untrusted_devices)
if not devices:
return False
untrusted_supersedes = untrusted_chainlink['signed']['supersedes']
untrusted_signee = untrusted_chainlink['signee']
untrusted_signature = untrusted_chainlink['signature']
if is_base64_string(untrusted_supersedes, allow_none = True):
supersedes = untrusted_supersedes
else: return False
if is_base64_string(untrusted_signee, allow_none = True):
signee = untrusted_signee
else: return False
if is_base64_string(untrusted_signature, allow_none = True):
signature = untrusted_signature
else: return False
chainlink = {
'signed': {
'devices': devices,
'supersedes': supersedes,
},
'signee': signee,
'signature': signature,
}
return chainlink
def clean_chain(untrusted_chain):
chain = []
for untrusted_chainlink in untrusted_chain:
chainlink = clean_chainlink(untrusted_chainlink)
if chainlink:
chain.append(chainlink)
else: return False
return chain
def json_serialize_canonical(object):
serialized = json.dumps(object,
sort_keys = True,
indent = None,
separators = (',', ':'),
ensure_ascii = False,
allow_nan = False,
)
return serialized
def crypto_hash(bytes):
digest = hashes.Hash(hashes.SHA256())
digest.update(bytes)
hash = digest.finalize()
return(hash)
def crypto_hash_human(string):
bytes = string.encode('utf-8')
hashed = crypto_hash(bytes)
hashed_base64 = base64.b64encode(hashed).decode('utf-8')
return(hashed_base64)
def object_json_canonical_bytes(signing_object):
signing_string = json_serialize_canonical(signing_object)
signing_bytes = signing_string.encode('utf-8')
return signing_bytes
def signature_verify(publickey_string, signature_string, message):
publickey_binary = base64.b64decode(publickey_string)
publickey = serialization.load_der_public_key(publickey_binary)
signature = base64.b64decode(signature_string)
try:
publickey.verify(
signature,
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
return True
except InvalidSignature:
return False
def chainlink_verify(chainlink, chainlink_previous):
def chainlink_integrity(chainlink, link_hash_claim):
link_json_canonical = json_serialize_canonical(chainlink)
link_hash = crypto_hash_human(link_json_canonical)
if link_hash == link_hash_claim:
return True
else:
log("Claimed: " + link_hash_claim)
log("Actual: " + link_hash)
return False
# Is the publickey present in the previous chainlink?
publickey = chainlink['signee']
if not publickey in chainlink_previous['signed']['devices']:
log("Signature was not present in ")
return False
# Is the signature correct?
signature = chainlink['signature']
signing_object = chainlink['signed']
signing_bytes = object_json_canonical_bytes(signing_object)
if not signature_verify(publickey, signature, signing_bytes):
return False
# Not we are allowed to interact with the signed part of the object
# Calculate whether the hash from the previous chainlink is correct
link_hash_claim = chainlink['signed']['supersedes']
if not chainlink_integrity(chainlink_previous, link_hash_claim):
return False
return True
def chain_verify(chain):
# Make sure that the first chainlink does not reference an earlier one
if chain[0]['signed']['supersedes'] is not None:
return False
if chain[0]['signature'] is not None:
return False
if chain[0]['signee'] is not None:
return False
# Iterate, skipping the first element, making the counter start there too
for link_index, chainlink in enumerate(chain[1:], 1):
chainlink_previous = chain[link_index - 1]
if not chainlink_verify(chainlink, chainlink_previous):
return False
return True
def select(dict_items, message):
selection_counter = 1
for key, value in dict_items:
print("{}: {}".format(selection_counter, key))
selection_counter += 1
selection_int = 0
message_wrong_input = "Please enter a number corresponding to one of the options above."
while True:
selection_string = input(message)
if selection_string.isnumeric():
selection_int = int(selection_string)
else:
log(message_wrong_input)
continue
if 0 < selection_int <= len(dict_items):
break
else:
log(message_wrong_input)
# Return, from the appropriate dictionary item, the corresponding value
return dict_items[selection_int - 1][1]
def device_add(
dirty_chainlink_previous,
device_new_key_public_string,
device_new_name,
**kwargs
):
skip_cleaning = kwargs.pop('skip_cleaning', False)
if skip_cleaning:
chainlink = dirty_chainlink_previous
else:
chainlink = clean_chainlink(dirty_chainlink_previous)
# Verify the speficied public key, and add device to chainlink
if is_base64_string(device_new_key_public_string):
chainlink['signed']['devices'][device_new_key_public_string] = device_new_name
else: return False
return chainlink
def chain_create(device_publickey_string, device_name):
chainlink = {
'signed': {
'devices': {},
'supersedes': None,
},
'signature': None,
'signee': None,
}
chainlink = device_add(
chainlink,
device_publickey_string,
device_name,
skip_cleaning = True,
)
chain = [ chainlink ]
return chain
def key_private_load_file(key_filename):
with open(key_filename, 'rb') as key_file:
privatekey = serialization.load_der_private_key(
key_file.read(),
password = None,
)
return(privatekey)
def get_publickey_base64(privatekey):
key_public = privatekey.public_key()
key_public_der = key_public.public_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
key_public_base64 = base64.b64encode(key_public_der).decode('utf-8')
return(key_public_base64)
def chainlink_sign(privatekey, chainlink):
def sign_base64(privatekey, message):
signature = privatekey.sign(
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
signature_base64 = base64.b64encode(signature).decode('utf-8')
return(signature_base64)
signing_object = chainlink['signed']
signing_bytes = object_json_canonical_bytes(signing_object)
signature = sign_base64(privatekey, signing_bytes)
chainlink['signature'] = signature
chainlink['signee'] = get_publickey_base64(privatekey)
return chainlink