demo/secure_element/resources/generation_scripts/setup_demo.py

1 #
2 # EVALUATION AND USE OF THIS SOFTWARE IS SUBJECT TO THE TERMS AND
3 # CONDITIONS OF THE CONTROLLING LICENSE AGREEMENT FOUND AT LICENSE.md
4 # IN THIS SDK. IF YOU DO NOT AGREE TO THE LICENSE TERMS AND CONDITIONS,
5 # PLEASE RETURN ALL SOURCE FILES TO SILICON LABORATORIES.
6 # (c) Copyright 2018, Silicon Laboratories Inc. All rights reserved.
7 #
8 
9 from __future__ import print_function
10 import sys
11 import os
12 import optparse
13 import traceback
14 import subprocess
15 from collections import OrderedDict
16 
17 
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
24 
25 
26 
27 openssl_handle = None
28 
29 
30 
31 ''' ******************************************************************************************* '''
32 def setup_demo(hostname, start_at_step=1):
33  print('Opening connection to device')
34  with secure_element_device() as device:
35  print(' Connected')
36  _setup_demo(device, hostname, start_at_step)
37 
38 
39 
40 
41 
42 ''' ******************************************************************************************* '''
43 def _step1_generate_self_signed_root_ca(**kwargs):
44  create_root_ca()
45 
46 
47 ''' ******************************************************************************************* '''
48 def _step2_generate_server_cert(**kwargs):
49  create_server_cert(hostname=kwargs['hostname'])
50 
51 
52 ''' ******************************************************************************************* '''
53 def _step3_generate_signer_cert(**kwargs):
54  create_signer_cert()
55 
56 
57 ''' ******************************************************************************************* '''
58 def _step4_generate_device_cert(**kwargs):
59  _create_device_cert(device=kwargs['device'])
60 
61 
62 ''' ******************************************************************************************* '''
63 def _step5_reprogram_device(**kwargs):
64  print('Rebuild and program the application AND resources to your device now')
65 
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')
69 
70  sys.stdin.readline()
71 
72 
73 ''' ******************************************************************************************* '''
74 def _step6_save_credentials_to_se(**kwargs):
75  device=kwargs['device']
76 
77  print('Reconnecting to device')
78  device.disconnect()
79  device.connect()
80 
81  _provision_device(device, hostname=kwargs['hostname'])
82 
83 
84 ''' ******************************************************************************************* '''
85 def _step7_start_testing_server(**kwargs):
86  global openssl_handle
87 
88  script_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'openssl_server'))
89  if os.name == 'nt':
90  script_path += '.bat'
91 
92  else:
93  script_path += '.sh'
94 
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')
98 
99  try:
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')
103 
104 
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')
109  print('OR\n')
110  print('set wlan.ssid <your network name>')
111  print('set wlan.passkey <your network password>')
112  print('save')
113  print('network_up')
114 
115  print('\nOnce the device connects to the network it will begin issuing HTTPS requests to the testing server\n\n')
116 
117 
118 
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
128 
129 
130 ''' ******************************************************************************************* '''
131 def _setup_demo(device, hostname, start_at_step):
132  print("\nRetrieving device's status ...")
133  status = device.get_status()
134 
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')
142  sys.exit(1)
143 
144  if start_at_step != 1:
145  print('\nStarting at step %d: %s' % (start_at_step, SETUP_STEPS.keys()[start_at_step-1]))
146 
147 
148  step_count = 1
149  for step_name, step_func in SETUP_STEPS.items():
150  if step_count != start_at_step:
151  step_count += 1
152  continue
153  start_at_step += 1
154 
155  print('\n=== Executing step %d: %s ===\n' % (step_count, step_name))
156  step_func(device=device, hostname=hostname)
157  step_count += 1
158 
159 
160  print('\nSuccess')
161  print('Your device is now setup and ready to run the demo.\n\n')
162 
163  if openssl_handle:
164  print('If the OpenSSL testing server was started, the following is its output:')
165  try:
166  openssl_handle.wait()
167  except:
168  openssl_handle.kill()
169 
170 
171 
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',
178  type='int',
179  default=1,
180  help='Start at specific setup step')
181  parser.add_option('--debug',
182  action='store_true',
183  default=False,
184  help='Print stack trace on failure')
185 
186  options, _ = parser.parse_args()
187 
188  if not options.hostname:
189  raise Exception('Must provide --hostname argument')
190 
191  try:
192  setup_demo(hostname=options.hostname,
193  start_at_step=options.start_at_step)
194  except Exception as e:
195  if options.debug:
196  traceback.print_exc()
197  print(e)