This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doord.py
executable file
·1125 lines (979 loc) · 49.7 KB
/
doord.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 python2
import sys
import os
import syslog
import json
import base64
import time
from math import ceil
from multiprocessing import Process, Manager, Lock
import crc16
import bcrypt
import requests
import RPi.GPIO as gpio
sys.path.append("MFRC522-python")
import MFRC522
import paho.mqtt.client
class DoorService:
SERVER_POLL = 300 # seconds
DOOR_IO = 15 # pi numbering
SWITCH_IO = 11
LED_IO = 18
LED_HEARTBEAT_TIMES = (0.1, 5) # on, off time in seconds
LED_DOOR_OPEN_TIMES = (0.25, 0.25)
LED_MAGIC_TAG_TIMES = (0.1, 0.1)
MAGIC_TAG_TIMEOUT = 10 # seconds
DEFAULT_SECTOR_A = 1
DEFAULT_SECTOR_B = 2
mqtt = None
def __init__(self):
try:
rcfile = open('doorrc', 'r')
except IOError:
print "Can't read file: 'doorrc', you need that."
sys.exit(1)
self.settings = json.loads(rcfile.read())
rcfile.close()
self.DOOR_OPEN_TIME = 5 # seconds
self.MAGIC_TAGS = {self.settings['init_tag_id']: "init_tag", # 4-byte UIDs of "magic" tags that cause the doord to perform actions
"b": "init_tag_safe", # NEVER set this one
self.settings['pull_db_tag_id']: "pull_db"}
self.nfc = MFRC522.MFRC522()
self.db = EntryDatabase(self.settings['server_url'], self.settings['api_key'])
self.MEMBER = self.settings['member_role_id']
self.KEYHOLDER = self.settings['keyholder_role_id']
self.LOCATION = self.settings['location_name']
self.recent_tags = {}
self.last_server_poll = os.times()[4] # EntryDatabase will force a blocking poll when instantiated
self.door_opened = False
self.led_last_time = os.times()[4]
gpio.setmode(gpio.BOARD)
gpio.setup(self.DOOR_IO, gpio.OUT)
gpio.output(self.DOOR_IO, gpio.LOW)
gpio.setup(self.LED_IO, gpio.OUT)
gpio.output(self.LED_IO, gpio.LOW)
gpio.setup(self.SWITCH_IO, gpio.IN, pull_up_down=gpio.PUD_UP)
if 'mqtt' in self.settings and self.settings['mqtt']['server'] is not None:
self.mqtt = paho.mqtt.client.Client()
self.mqtt.on_connect = self.mqtt_on_connect
if self.settings['mqtt']['secure'] is True:
self.mqtt.tls_set(("/etc/ssl/certs/ca-certificates.crt"))
elif self.settings['mqtt']['secure']:
self.mqtt.tls_set((self.settings['mqtt']['secure']))
if 'user' in self.settings['mqtt']:
password = None
if 'password' in self.settings['mqtt']:
password = self.settings['mqtt']['password']
self.mqtt.username_pw_set(self.settings['mqtt']['user'], password=password)
if 'port' not in self.settings['mqtt']:
self.settings['mqtt']['port'] = 1883
self.mqtt.connect(self.settings['mqtt']['server'],
port=self.settings['mqtt']['port'])
self.mqtt.loop_start()
print "Initialised"
def __del__(self):
gpio.cleanup()
if self.mqtt is not None:
self.mqtt.disconnect()
self.mqtt.loop_stop()
def main(self):
while True:
self._iter()
self._server_iter()
self._led_iter()
def _iter(self):
(status, TagType) = self.nfc.MFRC522_Request(self.nfc.PICC_REQIDL) # searches for a card for up to approxamately 100ms
# is a device presented
if status == self.nfc.MI_OK:
# run anti-collision and let the next id fall out
(status, uid) = self.nfc.MFRC522_Anticoll()
if status == self.nfc.MI_OK:
tag = Tag(uid, self.nfc, self.db)
if (str(tag)) in self.MAGIC_TAGS:
self.magic_tag(self.MAGIC_TAGS[str(tag)])
return
if str(tag) in self.recent_tags:
if self.recent_tags[str(tag)] + self.DOOR_OPEN_TIME > os.times()[4]:
del tag
return # ignore a tag for DEBOUNCE seconds after sucessful auth
gpio.output(self.LED_IO, gpio.HIGH)
print "Found tag UID: " + str(tag)
# authenticate
# lock database first to keep updates whole
self.db.lock.acquire()
(status, roles) = tag.authenticate()
if status:
self.recent_tags[str(tag)] = os.times()[4]
if (self.KEYHOLDER in roles) or (self.MEMBER in roles and gpio.input(self.SWITCH_IO) == 0):
# open the door
print "Tag " + str(tag) + " authenticated"
tag.log_auth(self.LOCATION, "allowed")
self.door_opened = os.times()[4]
gpio.output(self.DOOR_IO, gpio.HIGH)
if self.mqtt is not None:
self.mqtt.publish('{}/{}'.format(self.settings['mqtt']['topic'].rstrip('/'), str(tag)),
payload=json.dumps({
"member_id": self.db.get_tag_user(str(tag)),
"member_name": self.db.get_user_name(self.db.get_tag_user(str(tag))),
"roles": self.db.get_user_roles(self.db.get_tag_user(str(tag)))
}),
qos=0,
retain=True)
else:
print "Tag " + str(tag) + " authenticated but NOT keyholder"
tag.log_auth(self.LOCATION, "denied")
# update the server with tag counts and scans early
self.db.server_poll()
self.last_server_poll = os.times()[4]
else:
print "Tag " + str(tag) + " NOT authenticated"
self.db.lock.release()
del tag
gpio.output(self.LED_IO, gpio.LOW)
self.led_last_time = os.times()[4]
else:
print "Failed to read UID"
if self.door_opened and os.times()[4] > self.door_opened + self.DOOR_OPEN_TIME:
# close the door
gpio.output(self.DOOR_IO, gpio.LOW)
self.door_opened = False
def _server_iter(self):
if os.times()[4] > self.last_server_poll + self.SERVER_POLL:
self.db.server_poll()
self.last_server_poll = os.times()[4]
def _led_iter(self):
if gpio.input(self.DOOR_IO):
# door open
(ledon, ledoff) = self.LED_DOOR_OPEN_TIMES
else:
(ledon, ledoff) = self.LED_HEARTBEAT_TIMES
if gpio.input(self.LED_IO):
# LED on
if os.times()[4] > self.led_last_time + ledon:
gpio.output(self.LED_IO, gpio.LOW)
self.led_last_time = os.times()[4]
else:
# LED off
if os.times()[4] > self.led_last_time + ledoff:
gpio.output(self.LED_IO, gpio.HIGH)
self.led_last_time = os.times()[4]
def magic_tag(self, function):
if function is "init_tag":
self.init_tag()
elif function is "init_tag_safe":
self.init_tag(sector_keys="safe")
elif function is "pull_db":
self.db.server_poll()
self.last_server_poll = os.times()[4]
def init_tag(self, sector_keys="production"):
print "Entered tag init mode: " + str(sector_keys)
magic_tag_start = os.times()[4]
while True:
# magic tag timeout
if os.times()[4] > magic_tag_start + self.MAGIC_TAG_TIMEOUT:
gpio.output(self.LED_IO, gpio.LOW)
self.led_last_time = os.times()[4]
return
(status, TagType) = self.nfc.MFRC522_Request(self.nfc.PICC_REQIDL)
# is a device presented
if status == self.nfc.MI_OK:
# run anti-collision and let the next id fall out
(status, uid) = self.nfc.MFRC522_Anticoll()
if status == self.nfc.MI_OK:
tag = Tag(uid, self.nfc, self.db)
# is it the magic tag still?
if str(tag) in self.MAGIC_TAGS:
del tag
continue
print "Found tag: " + str(tag)
gpio.output(self.LED_IO, gpio.HIGH)
self.db.lock.acquire()
try:
if sector_keys is "production":
tag.initialize(self.DEFAULT_SECTOR_A, self.DEFAULT_SECTOR_B)
else:
tag.initialize(self.DEFAULT_SECTOR_A, self.DEFAULT_SECTOR_B, sector_keys="safe")
except Exception as e:
print "Problem initializing tag: " + str(e)
self.db.lock.release()
gpio.output(self.LED_IO, gpio.LOW)
self.led_last_time = os.times()[4]
del tag
return
# led blinking while waiting for a tag
(ledon, ledoff) = self.LED_MAGIC_TAG_TIMES
if gpio.input(self.LED_IO):
# LED on
if os.times()[4] > self.led_last_time + ledon:
gpio.output(self.LED_IO, gpio.LOW)
self.led_last_time = os.times()[4]
else:
# LED off
if os.times()[4] > self.led_last_time + ledoff:
gpio.output(self.LED_IO, gpio.HIGH)
self.led_last_time = os.times()[4]
def mqtt_on_connect(self, client, userdata, flags, rc):
print("Connected to MQTT server: {}:{}".format(self.settings['mqtt']['server'], self.settings['mqtt']['port']))
class Tag:
BCRYPT_BASE64_DICT = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
BCRYPT_VERSION = ['2', '2a', '2b', '2y']
BCRYPT_COST = 8 # tuned for performance, we must hash 4 times per authentication, this could be reduced to 3 if needed
SECTOR_LOCK_BYTES = [0x7F, 0x07, 0x88, 0x69] # key a r/w, key b r/w/conf
DEFAULT_KEY = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] # the key locking un-written cards
SAFE_A_KEY = [0x6B, 0x65, 0x79, 0x20, 0x61, 0x00] # safe keys used for testing "key a" in ascii
SAFE_B_KEY = [0x6B, 0x65, 0x79, 0x20, 0x62, 0x00] # "key b"
# create tag object representing one session with one tag
def __init__(self, uid, nfc, db):
self.count = None # the correct count
self.count_a = None # counts read from tag
self.count_b = None
self.uid = uid
self.nfc = nfc
self.db = db
self.select()
def select(self):
self.nfc.MFRC522_SelectTag(self.uid)
def unselect(self):
self.nfc.MFRC522_StopCrypto1()
def __del__(self):
self.unselect()
# attempt the whole authentication process with this tag
def authenticate(self):
sector_a_ok = True
sector_b_ok = True
try:
userid = self.db.get_tag_user(str(self))
except EntryDatabaseException as e:
if str(e) == "Unkown tag":
print "Tag " + str(self) + " is alien"
elif str(e) == "Unassigned tag":
print "Tag " + str(self) + " is not assigned to anyone"
else:
print "Database error, could not load user id for tag: " + str(e)
return (False, [])
try:
username = self.db.get_user_name(userid)
except EntryDatabaseException as e:
print "Database error, could not load user name: " + str(e)
return (False, [])
print "Tag is assigned to user " + userid + " (" + username + ")"
try:
self.count = self.db.get_tag_count(str(self))
except EntryDatabaseException as e:
print "Database error, could not load tag count: " + str(e)
return (False, [])
try:
sector_a_data = self.read_sector(self.db.get_tag_sector_a_sector(str(self)),
self.db.get_tag_sector_a_key_b(str(self)),
self.nfc.PICC_AUTHENT1B)
sector_b_data = self.read_sector(self.db.get_tag_sector_b_sector(str(self)),
self.db.get_tag_sector_b_key_b(str(self)),
self.nfc.PICC_AUTHENT1B)
except TagException as e:
print "Failed to Read sector: " + str(e)
return (False, [])
except EntryDatabaseException as e:
print "Database error: " + str(e)
return (False, [])
try:
self.count_a = self.validate_sector(sector_a_data,
self.db.get_tag_sector_a_secret(str(self)))
except TagException as e:
print "Failed to validate sector a: " + str(e)
sector_a_ok = False
except EntryDatabaseException as e:
print "Database error: " + str(e)
return (False, [])
try:
self.count_b = self.validate_sector(sector_b_data,
self.db.get_tag_sector_b_secret(str(self)))
except TagException as e:
print "Failed to validate sector b: " + str(e)
sector_b_ok = False
except EntryDatabaseException as e:
print "Database error: " + str(e)
return (False, [])
if (sector_a_ok is False) and (sector_b_ok is False):
print "Failed to authenticate, both sectors invalid"
return (False, [])
if sector_a_ok and sector_b_ok and 1 < self.subtract(self.count_a, self.count_b) < 65535: # subtract() wraps in 16-bit positive space, -1 is 65535
print "Warning: valid sector counts spaced higher than expected: A: " + str(self.count_a) + " B: " + str(self.count_b)
if sector_a_ok and sector_b_ok and (self.count_a == self.count_b):
print "Warning: valid sector counts spaced lower than expected: A: " + str(self.count_a) + " B: " + str(self.count_b)
if (not sector_b_ok) or (sector_a_ok and self.greater_than(self.count_a, self.count_b)):
if self.less_than(self.count_a, self.count):
print "Duplicate tag detected, expected count: " + str(self.count) + ", tag count: " + str(self.count_a)
return (False, [])
if self.greater_than(self.count_a, self.count):
print "Tag ahead of expected count, expected: " + str(self.count) + ", tag count: " + str(self.count_a) + ", continueing"
try:
self.write_sector(self.db.get_tag_sector_b_sector(str(self)),
self.db.get_tag_sector_b_key_b(str(self)),
self.nfc.PICC_AUTHENT1B,
self.db.get_tag_sector_b_secret(str(self)),
self.plus(self.count_a, 1))
sector_b_backdata = self.read_sector(self.db.get_tag_sector_b_sector(str(self)),
self.db.get_tag_sector_b_key_b(str(self)),
self.nfc.PICC_AUTHENT1B)
readback = self.validate_sector(sector_b_backdata,
self.db.get_tag_sector_b_secret(str(self)))
except TagException as e:
print "Failed to update tag: " + str(e)
return (False, [])
except EntryDatabaseException as e:
print "Database error: " + str(e)
return (False, [])
if readback != (self.plus(self.count_a, 1)):
print "Tag readback not correct, expected: " + str(self.plus(self.count_a, 1)) + " Got: " + str(readback)
return (False, [])
self.db.set_tag_count(str(self), self.plus(self.count_a, 1)) # NEVER sucessfully authenticate without updating the count
else:
if self.less_than(self.count_b, self.count):
print "Duplicate tag detected, expected count: " + str(self.count) + ", tag count: " + str(self.count_b)
return (False, [])
if self.greater_than(self.count_b, self.count):
print "Tag ahead of expected count, expected: " + str(self.count) + ", tag count: " + str(self.count_b) + ", continuing"
try:
self.write_sector(self.db.get_tag_sector_a_sector(str(self)),
self.db.get_tag_sector_a_key_b(str(self)),
self.nfc.PICC_AUTHENT1B,
self.db.get_tag_sector_a_secret(str(self)),
self.plus(self.count_b, 1))
sector_a_backdata = self.read_sector(self.db.get_tag_sector_a_sector(str(self)),
self.db.get_tag_sector_a_key_b(str(self)),
self.nfc.PICC_AUTHENT1B)
readback = self.validate_sector(sector_a_backdata,
self.db.get_tag_sector_a_secret(str(self)))
except TagException as e:
print "Failed to update tag: " + str(e)
return (False, [])
except EntryDatabaseException as e:
print "Database error: " + str(e)
return (False, [])
if readback != (self.plus(self.count_b, 1)):
print "Tag readback not correct, expected: " + str(self.plus(self.count_b, 1)) + " Got: " + str(readback)
return (False, [])
self.db.set_tag_count(str(self), self.plus(self.count_b, 1)) # NEVER sucessfully authenticate without updating the count
roles = []
try:
roles = self.db.get_user_roles(self.db.get_tag_user(str(self)))
except EntryDatabaseException as e:
print "failed to get user roles: " + str(e)
return (True, roles)
def log_auth(self, location, result):
self.db.log_auth(str(self), location, result)
def initialize(self, sector_a_sector, sector_b_sector, sector_keys="production"):
if self.db.tag_in_db(str(self)):
raise TagException('DO NOT initialise a serial that is in the database, DOS or Breach may result.')
print "Generating random elements"
# bcrypt will reject a count padded with a null (chr(0)) character.
# It will also reject unicode text objects (u"hello") but not unicode
# characters in a regular string (b"You can't have unicode in python comments")
while True:
sector_a_secret = os.urandom(23) # 23 because this matches the entropy of the bcrypt digest itself
if b"\x00" not in sector_a_secret:
break
while True:
sector_b_secret = os.urandom(23)
if b"\x00" not in sector_b_secret:
break
# Mifare keys however, may contain zeros
if sector_keys == "production":
sector_a_key_a = map(ord, os.urandom(6))
sector_a_key_b = map(ord, os.urandom(6))
sector_b_key_a = map(ord, os.urandom(6))
sector_b_key_b = map(ord, os.urandom(6))
else:
print "WARNING: Using safe keys, this is NOT secure, please use production keys"
sector_a_key_a = self.SAFE_A_KEY
sector_a_key_b = self.SAFE_B_KEY
sector_b_key_a = self.SAFE_A_KEY
sector_b_key_b = self.SAFE_B_KEY
# if this failes anywhere we should remember the sector keys used
# so we can recover the tag
try:
print "Writing sector a: " + str(sector_a_sector)
self.write_sector(sector_a_sector,
self.DEFAULT_KEY,
self.nfc.PICC_AUTHENT1A,
sector_a_secret,
0)
print "Writing sector b: " + str(sector_b_sector)
self.write_sector(sector_b_sector,
self.DEFAULT_KEY,
self.nfc.PICC_AUTHENT1A,
sector_b_secret,
1)
print "Readback sectors"
sector_a_backdata = self.read_sector(sector_a_sector,
self.DEFAULT_KEY,
self.nfc.PICC_AUTHENT1A)
readback_a = self.validate_sector(sector_a_backdata,
sector_a_secret)
if readback_a != 0:
raise TagException("Sector a: " + str(sector_a_sector) + ", readback count incorect")
sector_b_backdata = self.read_sector(sector_b_sector,
self.DEFAULT_KEY,
self.nfc.PICC_AUTHENT1A)
readback_b = self.validate_sector(sector_b_backdata,
sector_b_secret)
if readback_b != 1:
raise TagException("Sector b: " + str(sector_b_sector) + ", readback count incorrect")
print "Securing sectors"
self.configure_sector(sector_a_sector,
self.DEFAULT_KEY,
self.nfc.PICC_AUTHENT1A,
sector_a_key_a,
self.SECTOR_LOCK_BYTES,
sector_a_key_b)
self.configure_sector(sector_b_sector,
self.DEFAULT_KEY,
self.nfc.PICC_AUTHENT1A,
sector_b_key_a,
self.SECTOR_LOCK_BYTES,
sector_b_key_b)
print "Readback sectors"
sector_a_backdata = self.read_sector(sector_a_sector,
sector_a_key_b,
self.nfc.PICC_AUTHENT1B)
readback_a = self.validate_sector(sector_a_backdata,
sector_a_secret)
if readback_a != 0:
raise TagException("Sector a: " + str(sector_a_sector) + ", readback count incorect")
sector_b_backdata = self.read_sector(sector_b_sector,
sector_b_key_b,
self.nfc.PICC_AUTHENT1B)
readback_b = self.validate_sector(sector_b_backdata,
sector_b_secret)
if readback_b != 1:
raise TagException("Sector b: " + str(sector_b_sector) + ", readback count incorrect")
print "Sending tag details to server"
self.db.set_tag_user(str(self), None)
self.db.set_tag_count(str(self), 1)
self.db.set_tag_sector_a_sector(str(self), sector_a_sector)
self.db.set_tag_sector_b_sector(str(self), sector_b_sector)
self.db.set_tag_sector_a_secret(str(self), sector_a_secret)
self.db.set_tag_sector_b_secret(str(self), sector_b_secret)
self.db.set_tag_sector_a_key_a(str(self), sector_a_key_a)
self.db.set_tag_sector_a_key_b(str(self), sector_a_key_b)
self.db.set_tag_sector_b_key_a(str(self), sector_b_key_a)
self.db.set_tag_sector_b_key_b(str(self), sector_b_key_b)
self.db.server_push_now()
except Exception as e:
print "sector a (" + str(sector_a_sector) + "):"
print " key a: " + str(sector_a_key_a)
print " key b: " + str(sector_a_key_b)
print "sector b (" + str(sector_b_sector) + "):"
print " key a: " + str(sector_b_key_a)
print " key b: " + str(sector_b_key_b)
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "FAILED TO WRITE TAG or UPDATE SERVER - WRITE DOWN THE KEYS SHOWN ABOVE AND STICK THEM TO THE TAG RIGHT NOW!!!!"
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
# make doubly sure this gets logged
syslog.syslog("doord: Failed to init a tag, keys attempted were: sector a (1): key a: " +
str(sector_a_key_a) + " key b: " + str(sector_a_key_b) + " sector b (2): key a: " +
str(sector_b_key_a) + " key b: " + str(sector_b_key_b))
raise
print "Successfully initialized tag"
# return a validated count stored in the sector data given or rasie exception
def validate_sector(self, sector_data, secret):
# Assert crc16 matches sector or log corrupt sector and return
payload = "".join(map(chr, sector_data[0:46]))
crc = (sector_data[46] << 8) + sector_data[47]
if not crc16.crc16xmodem(payload) == crc:
raise TagException("Sector data failed checksum")
count = (sector_data[0] << 8) + sector_data[1]
algorithm = sector_data[2]
cost = sector_data[3]
digest = self.unencode_bcrypt64(sector_data[4:44])
reserved = sector_data[44:46]
if algorithm > (len(self.BCRYPT_VERSION) - 1):
raise TagException("Unknown bcrypt algorithm: " + str(algorithm))
for b in reserved:
if b != 0:
raise TagException("Data in padding")
read_hash = '$' + str(self.BCRYPT_VERSION[algorithm]) + '$' + str(cost).zfill(2) + '$' + digest
try:
calculated_hash = bcrypt.hashpw(str(count) + str(secret), read_hash)
except ValueError as e:
raise TagException('Failed to make hash to compare: ' + str(e))
if calculated_hash != read_hash:
raise TagException("Hash does not match, count is not authentic.")
return count
# return data stored "securely" in given sector
def read_sector(self, sector, key, keyspec):
status = self.nfc.Auth_Sector(keyspec, sector, key, self.uid)
if (status != self.nfc.MI_OK):
raise TagException("Failed to authenticate sector " + str(sector) + " of Tag " + str(self))
(status, data) = self.nfc.Read_Sector(sector)
if (status != self.nfc.MI_OK):
raise TagException("Failed to read sector " + str(sector) + " of Tag " + str(self))
return data
def __str__(self):
return format(self.uid[0], "x").zfill(2) + format(self.uid[1], "x").zfill(2) + format(self.uid[2], "x").zfill(2) + format(self.uid[3], "x").zfill(2)
# write a sector to the tag given the count and secret
def write_sector(self, sector, key, keyspec, secret, count):
generated_hash = bcrypt.hashpw(str(count) + str(secret), bcrypt.gensalt(self.BCRYPT_COST))
hash_parts = generated_hash.split("$")
data = []
data.append(count >> 8)
data.append(count & 0xFF)
data.append(self.BCRYPT_VERSION.index(hash_parts[1])) # bcrypt algorithm
data.append(int(hash_parts[2])) # cost
data.extend(self.encode_bcrypt64(hash_parts[3])) # salt & digest
data.extend([0, 0]) # padding
crc = crc16.crc16xmodem("".join(map(chr, data))) # crc
data.append(crc >> 8)
data.append(crc & 0xFF)
status = self.nfc.Auth_Sector(keyspec, sector, key, self.uid)
if status != self.nfc.MI_OK:
raise TagException("Failed to authenticate sector " + str(sector) + " of Tag " + str(self))
(status, backData) = self.nfc.Write_Block(sector * 4, data[0:16])
if (status != self.nfc.MI_OK):
raise TagException("Failed to write sector " + str(sector) + " block " + str(sector * 4) + " of Tag " + str(self))
(status, backData) = self.nfc.Write_Block(sector * 4 + 1, data[16:32])
if (status != self.nfc.MI_OK):
raise TagException("Failed to write sector " + str(sector) + " block " + str(sector * 4 + 1) + " of Tag " + str(self))
(status, backData) = self.nfc.Write_Block(sector * 4 + 2, data[32:48])
if (status != self.nfc.MI_OK):
raise TagException("Failed to write sector " + str(sector) + " block " + str(sector * 4 + 2) + " of Tag " + str(self))
def configure_sector(self, sector, key, keyspec, key_a, lock_bytes, key_b):
status = self.nfc.Auth_Sector(keyspec, sector, key, self.uid)
if status != self.nfc.MI_OK:
raise TagException("Failed to authenticate sector " + str(sector) + " of Tag " + str(self))
data = []
data.extend(key_a)
data.extend(lock_bytes)
data.extend(key_b)
(status, backData) = self.nfc.Write_Block(sector * 4 + 3, data)
if (status != self.nfc.MI_OK):
raise TagException("Failed to write sector " + str(sector) + " block " + str(sector * 4 + 3) + " of Tag " + str(self))
# convert from binary byte array to bcrypt base64
def unencode_bcrypt64(self, binary_arr):
base64 = ""
i = 0
binary = 0
for c in binary_arr:
binary = binary + (c << i)
i += 8
for i in range(int(len(binary_arr) * 8 / 6.0)):
base64 = base64 + str(list(self.BCRYPT_BASE64_DICT)[(binary >> (i * 6)) & 63])
return base64
# convert from bcrypt base64 format to binary byte array
def encode_bcrypt64(self, base64):
binary_int = 0
i = 0
for c in list(base64):
binary_int = binary_int + (self.BCRYPT_BASE64_DICT.index(c) << i)
i += 6
binary = []
for i in range(int(ceil(len(list(base64)) * 6 / 8.0))):
binary.append(int(((binary_int >> (i * 8)) & 0xff)))
return binary
# 16-bit wrapping plus function
def plus(self, left, right):
return (left + right) % (2**16)
def subtract(self, left, right):
return (left - right) % (2**16)
# 16-bit wrapping greater than function
def greater_than(self, left, right):
left = (left + (2 ** 16 / 2)) % (2 ** 16)
right = (right + (2 ** 16 / 2)) % (2 ** 16)
if left == 0 and right == 65535:
return True
return left > right
# 16-bit wrapping less than function
def less_than(self, left, right):
left = (left + (2 ** 16 / 2)) % (2 ** 16)
right = (right + (2 ** 16 / 2)) % (2 ** 16)
if left == 65535 and right == 0:
return True
return left < right
class TagException(Exception):
pass
class EntryDatabase:
def __init__(self, server_url, api_key):
self.proc = None
self.mgr = Manager()
self.lock = Lock()
self.local = self.mgr.dict() # shared objects
self.unsent = self.mgr.dict() # updates we've never tried to send
self.send_queue = self.mgr.list() # list uf updates we're trying to send in order
self.server_url = None
self.api_key = None
self.server_url = server_url
self.api_key = api_key
# pull server copy down, retry forever here as we can do nothing
# until we have the db.
connected = False
while not connected:
print "Connecting to " + self.server_url
try:
self.server_pull_now()
connected = True
except EntryDatabaseException as e:
print "Error connecting to server: " + str(e)
time.sleep(60)
def __del__(self):
if type(self.proc) is Process and self.proc.is_alive():
self.proc.terminate()
# blocking pull of db from server
def server_pull_now(self):
try:
response = requests.get(self.server_url, cookies={'SECRET': self.api_key})
if response.status_code == requests.codes.ok:
self.local.clear()
self.local.update(json.loads(response.text))
else:
raise EntryDatabaseException("Server returned bad status to GET: " + str(response.status_code) +
" - " + str(response.text))
except requests.exceptions.RequestException as e:
raise EntryDatabaseException("GET request error: " + str(e))
# blocking update of server
def server_push_now(self):
self.send_queue.append(dict(self.unsent))
self.unsent.clear()
try:
while len(self.send_queue) > 0:
response = requests.post(self.server_url,
cookies={'SECRET': self.api_key},
data=json.dumps(self.send_queue[0]),
headers={'content-type': 'application/json'})
if response.status_code == requests.codes.ok:
self.send_queue.pop(0)
else:
raise EntryDatabaseException("Server returned bad status to POST:" + str(response.status_code) +
" - " + str(response.text) + " (send_queue: " +
str(len(self.send_queue)) + ")")
except requests.exceptions.RequestException as e:
raise EntryDatabaseException("POST request error: " + str(e)) + " (send_queue: " + \
str(len(self.send_queue)) + ")"
def _server_poll_worker(self):
if len(self.unsent) > 0:
# copy complete updates from unsent
self.lock.acquire()
self.send_queue.append(dict(self.unsent))
self.unsent.clear()
self.lock.release()
# try to send all the updates in order
try:
while len(self.send_queue) > 0:
response = requests.post(self.server_url,
cookies={'SECRET': self.api_key},
data=json.dumps(self.send_queue[0]),
headers={'content-type': 'application/json'})
if response.status_code == requests.codes.ok:
self.send_queue.pop(0)
else:
# there is nobody to catch anything thrown here
print "Server returned bad status to POST:" + str(response.status_code) + " (send_queue: " + str(len(self.send_queue)) + ")"
# do not pull if we fail to post, it may clobber unsent from local
# do not continue to push send_queue on a failure, it may deliver out of order
return
except requests.exceptions.RequestException as e:
print "POST request error: " + str(e) + " (send_queue: " + str(len(self.send_queue)) + ")"
# same warnings as bad status code above
return
try:
response = requests.get(self.server_url, cookies={'SECRET': self.api_key})
if response.status_code == requests.codes.ok:
self.lock.acquire()
# avoid re-declaring local as dict, small chance of a tag being read as alien here then working
# very soon after
self.local.clear()
self.local.update(json.loads(response.text))
self.lock.release()
else:
print "Server returned bad status to GET: " + str(response.status_code)
except requests.exceptions.RequestException as e:
print "GET request error: " + str(e)
# attempt to send unsent changes to the server and update the local copy, asynchronously
def server_poll(self):
if type(self.proc) is Process and self.proc.is_alive():
print "Server poll still in progress, not starting another."
else:
self.proc = Process(target=self._server_poll_worker)
self.proc.start()
def get_tag_user(self, uid):
try:
if 'tags' not in self.local or uid not in self.local['tags']:
raise EntryDatabaseException("Unkown tag")
if 'assigned_user' not in self.local["tags"][uid] or self.local["tags"][uid]["assigned_user"] is None:
raise EntryDatabaseException("Unassigned tag")
if type(self.local['tags'][uid]['assigned_user']) in [str, unicode]:
return self.local['tags'][uid]['assigned_user']
else:
raise EntryDatabaseException("Assigned user id not string: " + str(self.local['tags'][uid]['assigned_user']))
except TypeError as e:
raise EntryDatabaseException("TypeError: " + str(e))
def set_tag_user(self, uid, user):
p_local = dict(self.local) # copy and cast the shared object to a real dict before vivifying it
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'assigned_user'], user)
self.vivify(p_unsent, ['tags', uid, 'assigned_user'], user)
self.local.update(p_local) # update after to set the shared object to the content of the dict
self.unsent.update(p_unsent)
def get_tag_count(self, uid):
try:
if (type(self.local['tags'][uid]['count']) is int) and (0 <= self.local['tags'][uid]['count'] <= 65535):
return self.local['tags'][uid]['count']
else:
raise EntryDatabaseException("count not an int or out of range: " + str(self.local['tags'][uid]['count']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))
def vivify(self, dic, keys, value):
key = keys.pop(0)
if key not in dic:
if len(keys) < 1:
dic[key] = value
else:
dic.update({key: {}})
self.vivify(dic[key], keys, value)
else:
if len(keys) < 1:
dic[key] = value
else:
self.vivify(dic[key], keys, value)
def set_tag_count(self, uid, count):
p_local = dict(self.local)
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'count'], count)
self.vivify(p_unsent, ['tags', uid, 'count'], count)
self.local.update(p_local)
self.unsent.update(p_unsent)
def get_tag_sector_a_sector(self, uid):
try:
if type(self.local['tags'][uid]['sector_a_sector']) is int:
return self.local['tags'][uid]['sector_a_sector']
else:
raise EntryDatabaseException("sector_a_sector not an int: " + str(self.local['tags'][uid]['sector_a_sector']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))
def set_tag_sector_a_sector(self, uid, sector):
p_local = dict(self.local)
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'sector_a_sector'], sector)
self.vivify(p_unsent, ['tags', uid, 'sector_a_sector'], sector)
self.local.update(p_local)
self.unsent.update(p_unsent)
def get_tag_sector_b_sector(self, uid):
try:
if type(self.local['tags'][uid]['sector_b_sector']) is int:
return self.local['tags'][uid]['sector_b_sector']
else:
raise EntryDatabaseException("sector_b_sector not an int: " + str(self.local['tags'][uid]['sector_b_sector']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))
def set_tag_sector_b_sector(self, uid, sector):
p_local = dict(self.local)
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'sector_b_sector'], sector)
self.vivify(p_unsent, ['tags', uid, 'sector_b_sector'], sector)
self.local.update(p_local)
self.unsent.update(p_unsent)
def get_tag_sector_a_key_a(self, uid):
try:
if type(self.local['tags'][uid]['sector_a_key_a']) in [str, unicode]:
key = map(ord, base64.b64decode(self.local['tags'][uid]['sector_a_key_a']))
if len(key) == 6:
return key
else:
raise EntryDatabaseException("sector_a_key_a is incorrect length: " + str(key))
else:
raise EntryDatabaseException("sector_a_key_a not a string: " + str(self.local['tags'][uid]['sector_a_key_a']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))
def set_tag_sector_a_key_a(self, uid, key):
p_local = dict(self.local)
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'sector_a_key_a'], base64.b64encode("".join(map(chr, key))))
self.vivify(p_unsent, ['tags', uid, 'sector_a_key_a'], base64.b64encode("".join(map(chr, key))))
self.local.update(p_local)
self.unsent.update(p_unsent)
def get_tag_sector_a_key_b(self, uid):
try:
if type(self.local['tags'][uid]['sector_a_key_b']) in [str, unicode]:
key = map(ord, base64.b64decode(self.local['tags'][uid]['sector_a_key_b']))
if len(key) == 6:
return key
else:
raise EntryDatabaseException("sector_a_key_b is incorrect length: " + str(key))
else:
raise EntryDatabaseException("sector_a_key_b not a string: " + str(self.local['tags'][uid]['sector_a_key_b']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))
def set_tag_sector_a_key_b(self, uid, key):
p_local = dict(self.local)
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'sector_a_key_b'], base64.b64encode("".join(map(chr, key))))
self.vivify(p_unsent, ['tags', uid, 'sector_a_key_b'], base64.b64encode("".join(map(chr, key))))
self.local.update(p_local)
self.unsent.update(p_unsent)
def get_tag_sector_b_key_a(self, uid):
try:
if type(self.local['tags'][uid]['sector_b_key_a']) in [str, unicode]:
key = map(ord, base64.b64decode(self.local['tags'][uid]['sector_b_key_a']))
if len(key) == 6:
return key
else:
raise EntryDatabaseException("sector_b_key_a is incorrect length: " + str(key))
else:
raise EntryDatabaseException("sector_b_key_a not a string: " + str(self.local['tags'][uid]['sector_b_key_a']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))
def set_tag_sector_b_key_a(self, uid, key):
p_local = dict(self.local)
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'sector_b_key_a'], base64.b64encode("".join(map(chr, key))))
self.vivify(p_unsent, ['tags', uid, 'sector_b_key_a'], base64.b64encode("".join(map(chr, key))))
self.local.update(p_local)
self.unsent.update(p_unsent)
def get_tag_sector_b_key_b(self, uid):
try:
if type(self.local['tags'][uid]['sector_b_key_b']) in [str, unicode]:
key = map(ord, base64.b64decode(self.local['tags'][uid]['sector_b_key_b']))
if len(key) == 6:
return key
else:
raise EntryDatabaseException("sector_b_key_b is incorrect length: " + str(key))
else:
raise EntryDatabaseException("sector_b_key_b not a string: " + str(self.local['tags'][uid]['sector_b_key_b']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))
def set_tag_sector_b_key_b(self, uid, key):
p_local = dict(self.local)
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'sector_b_key_b'], base64.b64encode("".join(map(chr, key))))
self.vivify(p_unsent, ['tags', uid, 'sector_b_key_b'], base64.b64encode("".join(map(chr, key))))
self.local.update(p_local)
self.unsent.update(p_unsent)
def get_tag_sector_a_secret(self, uid):
try:
if type(self.local['tags'][uid]['sector_a_secret']) in [str, unicode]:
return base64.b64decode(self.local['tags'][uid]['sector_a_secret'])
else:
raise EntryDatabaseException("sector_a_secret is not string: " + str(self.local['tags'][uid]['sector_a_secret']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))
def set_tag_sector_a_secret(self, uid, secret):
p_local = dict(self.local)
p_unsent = dict(self.unsent)
self.vivify(p_local, ['tags', uid, 'sector_a_secret'], base64.b64encode(secret))
self.vivify(p_unsent, ['tags', uid, 'sector_a_secret'], base64.b64encode(secret))
self.local.update(p_local)
self.unsent.update(p_unsent)
def get_tag_sector_b_secret(self, uid):
try:
if type(self.local['tags'][uid]['sector_b_secret']) in [str, unicode]:
return base64.b64decode(self.local['tags'][uid]['sector_b_secret'])
else:
raise EntryDatabaseException("sector_b_secret is not string: " + str(self.local['tags'][uid]['sector_b_secret']))
except KeyError as e:
raise EntryDatabaseException("KeyError: " + str(e))