9 from __future__
import print_function
15 from collections
import OrderedDict
18 from secure_element_device
import secure_element_device
19 from create_root_ca
import create_root_ca
20 from create_server_cert
import create_server_cert
21 from create_signer_cert
import create_signer_cert
22 from create_device_cert
import _create_device_cert
23 from provision_device
import _provision_device
31 ''' ******************************************************************************************* ''' 32 def setup_demo(hostname, start_at_step=1):
33 print(
'Opening connection to device')
34 with secure_element_device()
as device:
36 _setup_demo(device, hostname, start_at_step)
42 ''' ******************************************************************************************* ''' 43 def _step1_generate_self_signed_root_ca(**kwargs):
47 ''' ******************************************************************************************* ''' 48 def _step2_generate_server_cert(**kwargs):
49 create_server_cert(hostname=kwargs[
'hostname'])
52 ''' ******************************************************************************************* ''' 53 def _step3_generate_signer_cert(**kwargs):
57 ''' ******************************************************************************************* ''' 58 def _step4_generate_device_cert(**kwargs):
59 _create_device_cert(device=kwargs[
'device'])
62 ''' ******************************************************************************************* ''' 63 def _step5_reprogram_device(**kwargs):
64 print(
'Rebuild and program the application AND resources to your device now')
66 print(
'After the app is programmed, press ENTER to continue this script\n')
67 print(
'NOTE: Be sure you download both the application AND resources to your device\n')
68 print(
'NOTE: Be sure the app is running on the device before pressing ENTER here\n')
73 ''' ******************************************************************************************* ''' 74 def _step6_save_credentials_to_se(**kwargs):
75 device=kwargs[
'device']
77 print(
'Reconnecting to device')
81 _provision_device(device, hostname=kwargs[
'hostname'])
84 ''' ******************************************************************************************* ''' 85 def _step7_start_testing_server(**kwargs):
88 script_path = os.path.normpath(os.path.join(os.path.dirname(__file__),
'..',
'openssl_server'))
95 print(
'Attempting to start OpenSSL testing server')
96 print(
' %s' % script_path)
97 print(
'\nNOTE: If this fails, then manually run the script from the command line')
100 openssl_handle = subprocess.Popen([script_path])
101 except Exception
as e:
102 print(
'Failed to start OpenSSL testing server, manually start the server from the command line')
105 ''' ******************************************************************************************* ''' 106 def _step8_connect_device_to_network(**kwargs):
107 print(
'\nFinally, open a serial terminal to the device and issue:\n')
108 print(
'network_up -s\n')
110 print(
'set wlan.ssid <your network name>')
111 print(
'set wlan.passkey <your network password>')
115 print(
'\nOnce the device connects to the network it will begin issuing HTTPS requests to the testing server\n\n')
119 SETUP_STEPS = OrderedDict()
120 SETUP_STEPS[
'Generate self-signed root CA cert'] = _step1_generate_self_signed_root_ca
121 SETUP_STEPS[
'Generate server cert'] = _step2_generate_server_cert
122 SETUP_STEPS[
'Generate signer cert'] = _step3_generate_signer_cert
123 SETUP_STEPS[
'Generate device cert'] = _step4_generate_device_cert
124 SETUP_STEPS[
'Re-program device'] = _step5_reprogram_device
125 SETUP_STEPS[
'Save credentials to SE'] = _step6_save_credentials_to_se
126 SETUP_STEPS[
'Start testing server'] = _step7_start_testing_server
127 SETUP_STEPS[
'Connect device to network'] = _step8_connect_device_to_network
130 ''' ******************************************************************************************* ''' 131 def _setup_demo(device, hostname, start_at_step):
132 print(
"\nRetrieving device's status ...")
133 status = device.get_status()
135 if not status.configured:
136 print(
'\n\nSecure Element is NOT configured!\n')
137 print(
'Press and hold button 1 to configure SE now')
138 print(
'WARN: This operation is irreversible!!')
139 print(
" Once the SE is programmed with this example's configuration,")
140 print(
' it cannot be programmed again!\n')
141 print(
'After configuring the SE, run this script again\n')
144 if start_at_step != 1:
145 print(
'\nStarting at step %d: %s' % (start_at_step, SETUP_STEPS.keys()[start_at_step-1]))
149 for step_name, step_func
in SETUP_STEPS.items():
150 if step_count != start_at_step:
155 print(
'\n=== Executing step %d: %s ===\n' % (step_count, step_name))
156 step_func(device=device, hostname=hostname)
161 print(
'Your device is now setup and ready to run the demo.\n\n')
164 print(
'If the OpenSSL testing server was started, the following is its output:')
166 openssl_handle.wait()
168 openssl_handle.kill()
172 ''' ******************************************************************************************* ''' 173 if __name__ ==
'__main__':
174 parser = optparse.OptionParser(description=
'This script sets up the device for the secure element demo')
175 parser.add_option(
'--hostname',
176 help=
"Required, Hostname of local testing server, typically this should be your computer's IP address. NOTE: You can also update your computer's 'hosts' file to use a domain")
177 parser.add_option(
'--start_at_step',
180 help=
'Start at specific setup step')
181 parser.add_option(
'--debug',
184 help=
'Print stack trace on failure')
186 options, _ = parser.parse_args()
188 if not options.hostname:
189 raise Exception(
'Must provide --hostname argument')
192 setup_demo(hostname=options.hostname,
193 start_at_step=options.start_at_step)
194 except Exception
as e:
196 traceback.print_exc()