-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt-experiment.py
474 lines (393 loc) · 15.2 KB
/
mqtt-experiment.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
import paho.mqtt.client as mqtt
import threading
import time
import json
from itertools import cycle
from random import choice
from datetime import datetime
from key_exchange_helper import *
from Graph import Graph;
from SecurityPolicyDatabase import SecurityPolicyDatabase
from SecurityAsscoiationDatabase import SecurityAssociationDatabase, SecurityAssociation
#from import bcolors
from logger import *
MQTT_ADDR = "127.0.0.1"
MQTT_PORT = 1883
class Server:
def __init__(self, spd, sad, channel_graph, server_id="key-server"):
self.spd = spd
self.sad = sad
self.channels = channel_graph
self.id = server_id
self.mqtt_con = None
self.message_loop = True
def connect(self):
self.mqtt_con = mqtt.Client(self.id)
self.mqtt_con.on_message = self.on_message
self.mqtt_con.connect(MQTT_ADDR, MQTT_PORT, keepalive=60, bind_address="")
def server_loop(self):
'''
Starts experiment by creating new sad entry for /*
'''
#root_sa = self.sad.create_sa()
#print_okgreen("Created Root node SA: " + str(root_sa))
self.mqtt_con.subscribe("#") #subscribe to all
while self.message_loop:
self.mqtt_con.loop()
#print_okblue("Message Loop: " + str(self.message_loop))
def on_message(self, client_id, userdata, message):
'''
Process new message packet
'''
print_okblue("---- {}:: Processing Message ---".format(self.id))
msg = str(message.payload.decode("utf-8"))
topic = message.topic
if msg == 'KILL-SIGNAL':
print_warning("GOT KILL SIGNAL, SETTING MESSAGE_LOOP FLAG TO FALSE")
self.message_loop = False
else:
self.process_message(msg, topic)
print_okblue("---- {}:: Finished Processing Message ---".format(self.id))
def process_message(self, msg_json, topic_str):
'''
example key request (payload), wrapped by message header
{
"client_id": 0,
"target": "KEY-REQUEST",
"payload": {
"target": 3
}
}
'''
if not msg_json or not topic_str:
return
msg = json.loads(msg_json)
if not validate_msg(msg):
print_fail("{}::Failed to process message:\n{}".format(self.id, msg_json))
return
source_id = msg['source_id']
payload = msg['payload']
target = msg['target']
if source_id == self.id:
return
#print_okblue("{} :: Received new valid message:\n{}".format(self.id, msg_json))
if target == 'KEY-REQUEST':
self.authenticate_client(source_id, payload, topic_str)
else:
print_warning("{} : WARNING: could not process message {}".format(self.id, msg_json))
def authenticate_client(self, client_id, payload, topic_str):
'''
TODO: remove client_id arg
authenticate a client request, if successful sends response topic
on by replacing ADVERTISEMENT token with RESPONSE token
authentication packet = {
target : "<node-id>",
client_id : "id of request client"
}
'''
if type(payload) == 'str':
request = parse_request_packet(payload)
else:
request = payload
if not validate_request_packet(request):
print_warning("Unable to validate request packet:\n{}".format(str(request)))
return
target = request['target']
sa = None
#Try to fetch sa for client and target
sa = self.sad.find_valid_sa(client_id, target)
if not sa: # NO sa in database, try to create a new one
print_warning("WARNING:: could not find sa for client" +
" [{}] for target [{}], trying to create one...".format(client_id, target))
if not sa: # NO sa in database, try to create a new one
sa = self.sad.create_sa(client_id, target)
if not sa:
print_warning("WARNING:: could not authenticate client" +
" [{}] for target [{}]".format(client_id, target))
return None
print_sad(self.sad, "{}-SAD".format(self.id))
# Send client new key
rsp_packet = create_response_packet(client_id, target, sa)
msg_packet = create_msg_packet(self.id, "RESPONSE", rsp_packet, json_encode=True)
rsp_topic = replace_topic_str(topic_str, "RESPONSE")
self.mqtt_con.publish(rsp_topic, msg_packet)
def _replace_topic_str(self, topic_str, new_target, seperator="/"):
topics = topic_str.split(seperator)
topics[-1] = new_target
return seperator.join(topics)
def loop(self):
print("loop")
if self.mqtt_con:
while True:
self.mqtt_con.loop()
print("error")
def time_loop(self, timeout=3):
seconds = 0
while(seconds < timeout):
print("Loop at T={}".format(str(seconds)))
self.mqtt_con.loop()
time.sleep(1)
seconds = seconds + 1
class Client:
def __init__(self, topic_group, client_id, spd, channel_graph):
print("Creating client: " + str(client_id))
self.topic_group = topic_group
self.id = client_id
self.spd = spd
self.sad = None
self.mqtt_con = None
self.channel_graph = channel_graph
self.message_loop = True
self.timestamp = 0 #used for measuring time between random requests
def connect(self):
self.sad = SecurityAssociationDatabase(self.spd, self.channel_graph)
self.mqtt_con = mqtt.Client("Client " + str(self.id))
self.mqtt_con.on_message = self.on_message
self.mqtt_con.connect(MQTT_ADDR, MQTT_PORT, keepalive=60, bind_address="")
self.mqtt_con.subscribe('#')
#client.connect(MQTT_ADDR, MQTT_PORT)
def on_message(self, client, userdata, message):
msg = str(message.payload.decode("utf-8"))
topic = message.topic
if msg == 'KILL-SIGNAL':
self.message_loop = False
else:
self.process_message(msg, topic)
def process_message(self, msg_json, topic_str):
'''
example key request (payload), wrapped by message header
{
"client_id": 0,
"target": "KEY-REQUEST",
"payload": {
"target": 3
}
}
'''
if not msg_json or not topic_str:
return
msg = json.loads(msg_json)
if not validate_msg(msg):
print_fail("Failed to process message: \n" + str(msg_json))
return
#print(msg)
source_id = msg['source_id']
payload = msg['payload']
target = msg['target']
if source_id == self.id:
return
print_okblue("Received new valid message:\n{}".format(msg_json))
if target == 'RESPONSE':
print_header("{}:: GOT RESPONSE MESSAGE".format(self.id))
self.handle_authentication_response(source_id, payload, topic_str)
elif target == 'PUBLISH':
print_header("{}:: GOT PUBLISH MESSAGE".format(self.id))
self.rcv_msg(source_id, payload)
else:
print_warning("{}:: WARNING: could not process message {}".format(self.id, msg_json))
def rcv_msg(self, source_id, msg):
#print_header("{} :: rcv_msg called : {}".format(self.id, msg))
if not validate_enc_message(msg):
print_fail(
"{}::ERROR:: could not validate encrypted message:\n{}".format(self.id, msg))
topic_id = msg['topic_id']
key = msg['key']
payload = msg['payload']
#check if topic is part of security policy
sp = self.spd.get_security_policy(self.id)
sp_keys = sp.keys()
if not topic_id in sp_keys:
print_fail("{}:: Topic not in secuirty policy dropping: {}".format(self.id, msg))
return None
#check sad for key,
sa_list = self.sad.key_lookup(topic_id)
if not sa_list: #find SA
self.request_sec_asoc(topic_id)
#print_warning(
# "{}:: Could not find SA for {}, making request...".format(self.id, topic_id))
else: # SA found
key_found = False
sec_asoc = None
for ind, sa in enumerate(sa_list): #find sa with matching key
if sa.key == key:
sec_asoc = sa
break
if not sec_asoc: # remove found sa and request a new one
self.sad.remove_security_association(sec_asoc)
print_warning(
"Could not find SA (stale keys) for" +
" {}, making request...".format(topic_id))
else:
print_okgreen("{}::Successfully decrypted message: {}".format(msg, self.id))
def handle_authentication_response(self, source_id, payload, topic_str):
response = None
if type(payload) == 'str':
response = parse_request_packet(payload)
else:
response = payload
if not response:
return None
if not validate_response_packet(response):
print_fail("WARNING:: Failed to validate response:\n{}".format(response))
return None
client_id = payload['client_id']
target = payload['target']
path = payload['path']
key = payload['key']
sa = SecurityAssociation(target, path, key)
print_header("[{}]::ADDING NEW SAD ENTRY {}".format(self.id, sa))
self.sad.add_security_association(target, sa)
print_sad(self.sad, "{}-SAD".format(self.id))
def choose_random_topic(self):
vertices = self.channel_graph.vertices()
r_node = choice(vertices)
return r_node
def random_request(self):
'''
Chooses random topic from channel graph
if sec asoc available, send message using key,
else request new security association
'''
topic_id = self.choose_random_topic()
sec_asoc = self.sad.find_valid_sa(self.id, topic_id)
if not sec_asoc:
self.request_sec_asoc(topic_id)
else:
#create new message
self.send_enc_packet(topic_id, "test-date")
def request_sec_asoc(self, topic_id):
''''
{
"client_id" : 0,
"target" : "KEY-REQUEST",
"payload" : {
"target" : 3
}
}
'''
sa_req = create_request_packet(topic_id, self.id)
#Create path to target
topic_str = self.channel_graph.get_topic_str(topic_id)
req_packet = create_request_packet(topic_id, self.id)
msg_json = create_msg_packet(self.id, "KEY-REQUEST",req_packet, json_encode=True)
print_header("Publishing [{}] on topic [{}]".format(msg_json, topic_str))
self.mqtt_con.publish(topic_str, msg_json)
def send_enc_packet(self, topic_id, msg, target='PUBLISH'):
'''
Send encrypted data packet,
if key available in sad for topic
use key, else request new key
'''
topic_str = self.channel_graph.get_topic_str(topic_id)
sec_asoc = self.sad.find_valid_sa(self.id, topic_id)
if not sec_asoc:
print_fail("Client::send_enc_packet cannot find security association")
return
enc_msg = create_enc_message(topic_id, sec_asoc.key, msg)
msg = create_msg_packet(self.id, 'PUBLISH', enc_msg, json_encode=True)
self.mqtt_con.publish(topic_str, msg)
def start(self):
self.mqtt_con.subscribe("#")
#print_fail("ERROR:: Client::start no implementation")
def loop(self):
while self.message_loop:
self.mqtt_con.loop()
def time_loop(self, timeout=3):
seconds = 0
while(seconds < timeout):
print("Loop at T={}".format(str(seconds)))
self.mqtt_con.loop()
time.sleep(1)
seconds = seconds + 1
def timed_random_choice(self, interval=1):
'''
interval is float value defining the number
of seconds because random requests
'''
c_time = time.time()
delta_time = c_time - self.timestamp
if delta_time > interval:
self.timestamp = time.time()
self.random_request()
def random_loop(self):
start = datetime.now()
while self.message_loop:
self.timed_random_choice()
self.mqtt_con.loop()
delta = datetime.now() - start
if delta.seconds > 10:
self.message_loop = False
#g = Graph(3,2)
#height = int(input("How many levels: "))
height = 3
#children = int(input("Number of children: "))
children = 2
#n_groups = int(input("Number of Groups/Policies: "))
#n_clients = int(input("Number of clients: "))
n_clients = 1
#testbed = input("Random(r)/Manual(m)")
def create_server_worker(server_id, spd, channel_graph):
print_okblue("--------------{}-STARTING...-------------".format(server_id))
s_sad = SecurityAssociationDatabase(spd, channel_graph)
server = Server(spd, s_sad, channel_graph, 'key-server')
server.connect()
server.server_loop()
print_okblue("--------------{}-EXIT-------------".format(server_id))
print_okblue("Server Done, exiting")
def create_client_worker(group, client_id, spd, channel_graph):
print_okblue("--------------{}-STARTED-----------------".format(client_id))
#create group
client = Client(group, client_id, spd, channel_graph)
client.connect()
client.start()
client.random_loop()
client.mqtt_con.publish("/0", "KILL-SIGNAL")
print_okblue("Client " + str(client_id) + " Done")
print_okblue("--------------{}-EXIT-----------------".format(client_id))
## Create channel graph and databases
channel_graph = Graph(height, children)
spd = SecurityPolicyDatabase()
sad = SecurityAssociationDatabase(spd, channel_graph)
#Create groups
client_groups = spd.create_group_r(5, channel_graph)
GROUPS = cycle(client_groups)
print(channel_graph)
root = channel_graph.get_node(0)
all_g = spd.create_group(root, channel_graph)
spd.add_security_policy('client-0',all_g)
#print spd
#start server
server_args = {
'server_id': 'keyserver',
'spd' : spd,
'channel_graph' : channel_graph
}
s_thread = threading.Thread(target=create_server_worker, kwargs=server_args)
s_thread.start()
#print("------------------")
#print("MAIN: Thread started")
#s_thread.join()
#print("STHREAD EXIT")
#print("------------------")
client_args = {
'spd': spd,
'channel_graph': channel_graph,
'client_id': None,
'group': None
}
client_args['client_id'] = 'client-0'
client_args['group'] = all_g
#spd.add_security_policy('client-0', group)
c_thread = threading.Thread(target=create_client_worker, kwargs=client_args)
c_thread.start()
client_args['client_id'] = 'client-1'
client_args['group'] = GROUPS.__next__()
spd.add_security_policy('client-1',client_args['group'])
print_spd(spd)
print(client_args['group'])
d_thread = threading.Thread(target=create_client_worker, kwargs=client_args)
d_thread.start()
d_thread.join()
c_thread.join()
s_thread.join()
print_okgreen("-------------DONE--------------")