-
Notifications
You must be signed in to change notification settings - Fork 55
/
set_jade_pinserver.py
executable file
·124 lines (102 loc) · 4.24 KB
/
set_jade_pinserver.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
#!/usr/bin/env python
import sys
import logging
import argparse
from hashlib import sha256
from jadepy import JadeAPI
# Enable jade logging
jadehandler = logging.StreamHandler()
logger = logging.getLogger('jadepy.jade')
logger.setLevel(logging.INFO)
logger.addHandler(jadehandler)
device_logger = logging.getLogger('jadepy.jade-device')
device_logger.setLevel(logging.INFO)
device_logger.addHandler(jadehandler)
def update_pinserver(jade, args):
# do any reset first/separately
if args.reset_details or args.reset_certificate:
jade.reset_pinserver(args.reset_details, args.reset_certificate)
pubkey = None
if args.pubkeyfile:
with open(args.pubkeyfile, 'rb') as f:
pubkey = f.read()
print('Sending pubkey: ', pubkey.hex())
certificate = None
if args.certfile is not None:
if args.certfile:
with open(args.certfile, 'r') as f:
certificate = f.read()
hash = sha256(certificate.encode()).hexdigest()
print('Sending certificate with hash:', hash)
else:
certificate = ''
print('Sending no-certificate-required')
# Do it!
jade.set_pinserver(args.urlA, args.urlB, pubkey, certificate)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
conns = parser.add_mutually_exclusive_group()
conns.add_argument('--serialport',
action='store',
dest='serialport',
help='Serial port or device - only pass if default not correct',
default=None)
conns.add_argument('--bleid',
action='store',
dest='bleid',
help='BLE device serial number or id - only pass if connecting via BLE',
default=None)
parser.add_argument('--reset-details',
action='store_true',
dest='reset_details',
help='Reset pinserver details to default',
default=False)
parser.add_argument('--reset-certificate',
action='store_true',
dest='reset_certificate',
help='Reset pinserver certificate to default',
default=False)
parser.add_argument('--set-url',
action='store',
dest='urlA',
help='URL for pinserver',
default=None)
parser.add_argument('--set-url2',
action='store',
dest='urlB',
help='Second URL for pinserver',
default=None)
parser.add_argument('--set-pubkey',
action='store',
dest='pubkeyfile',
help='Binary file containing the pinserver public key',
default=None)
parser.add_argument('--set-certificate',
action='store',
dest='certfile',
help='Text .pem file for any pinserver TLS/URL root certificate'
' - use empty if no certificate required',
default=None)
parser.add_argument('--log',
action='store',
dest='loglevel',
help='Jade logging level',
choices=['DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL'],
default='INFO')
args = parser.parse_args()
jadehandler.setLevel(getattr(logging, args.loglevel))
logger.debug(f'args: {args}')
# Cannot update 'second' url unless setting first url
if args.urlA is None and args.urlB is not None:
print("Cannot set second URL unless also setting first URL")
sys.exit(1)
print('Connecting...')
if args.bleid:
create_jade_fn = JadeAPI.create_ble
kwargs = {'serial_number': args.bleid}
else:
create_jade_fn = JadeAPI.create_serial
kwargs = {'device': args.serialport, 'timeout': 120}
with create_jade_fn(**kwargs) as jade:
print('Connected: {}'.format(jade.get_version_info()))
update_pinserver(jade, args)