-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite_signed_ndef_on_desfire.py
executable file
·67 lines (58 loc) · 1.85 KB
/
write_signed_ndef_on_desfire.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
#!/usr/bin/env python2
import sys
import os
import tempfile
from binascii import hexlify
from nfc_smartcard import NFCDevice, NFCError
from sign_uid import signUid
if len(sys.argv) < 2:
print "Usage: write_signed_ndef_on_desfire.py private_key_in_hex"
sys.exit(3)
tempFd = None
tempFname = None
try:
print "Opening NFC reader"
nfc = NFCDevice()
nfc.pollNr = 0xFF #poll indefinitely
print "Waiting for Desfire card to appear in the reader"
uid_hex = hexlify(nfc.scanUID())
key = sys.argv[1].decode("hex")
print "Got UID %s" % uid_hex
signature = signUid(key, uid_hex.decode("hex"))
(tempFd, tempFname) = tempfile.mkstemp(dir="/tmp")
signatureJson = '{"brmdoorSignature": "%s"}' % signature.encode("hex")
print "Writing signature JSON:", signatureJson
os.write(tempFd, signatureJson)
os.close(tempFd)
print "Wrote signature into %s" % tempFname
except NFCError, e:
#this exception happens also when scanUID times out
print("Failed to wait for Desfire card: %s" % e)
if tempFname:
os.unlink(tempFname)
sys.exit(1)
except Exception, e:
print("Something went wrong when writing the signature to file:", e)
if tempFname:
os.unlink(tempFname)
sys.exit(2)
finally:
nfc.close()
nfc.unload()
# We'll just call the command line tools so that we don't need to copy&paste the NDEF writing code to nfc_smartcard.cpp
print "Formatting card"
res = os.system("mifare-desfire-format -y")
if res != 0:
print "Formatting failed"
sys.exit(4)
print "Creating NDEF file/application"
res = os.system("mifare-desfire-create-ndef -y")
if res != 0:
print "Creating NDEF failed"
sys.exit(4)
print "Writing NDEF with signature onto Desfire"
res = os.system("mifare-desfire-write-ndef -y -i %s" % tempFname)
if res != 0:
print "Writing NDEF failed"
sys.exit(4)
print "All done"