Skip to content

Commit b1dfd13

Browse files
krish2718kartben
authored andcommitted
scripts: utils: tls_creds_installer: Improvements
Improve error handling and reporting. Signed-off-by: Chaitanya Tata <[email protected]>
1 parent 7f0cd66 commit b1dfd13

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

scripts/utils/tls_creds_installer.py

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,28 @@ def write_credential(self, sectag, cred_type, cred_text):
6565
for c in range(chunks):
6666
chunk = encoded[c * TLS_CRED_CHUNK_SIZE : (c + 1) * TLS_CRED_CHUNK_SIZE]
6767
self.write_raw(f"cred buf {chunk}")
68-
self.serial_wait_for_response("Stored")
68+
result, output = self.serial_wait_for_response("Stored", "RX ring buffer full")
69+
if not result:
70+
logging.error("Failed to store chunk in the device: unknown error")
71+
if output and b"RX ring buffer full" in output:
72+
logging.error(f"Failed to store chunk in the device: {output}")
73+
return False
74+
if not 0 <= cred_type < len(TLS_CRED_TYPES):
75+
logger.error(
76+
f"Invalid credential type: {cred_type}. Range [0, {len(TLS_CRED_TYPES) - 1}]."
77+
)
78+
return False
6979
self.write_raw(f"cred add {sectag} {TLS_CRED_TYPES[cred_type]} DEFAULT bint")
70-
result, _ = self.serial_wait_for_response("Added TLS credential")
80+
result, _ = self.serial_wait_for_response("Added TLS credential", "already exists")
7181
time.sleep(1)
7282
return result
7383

7484
def delete_credential(self, sectag, cred_type):
85+
if not 0 <= cred_type < len(TLS_CRED_TYPES):
86+
logger.error(
87+
f"Invalid credential type: {cred_type}. Range [0, {len(TLS_CRED_TYPES) - 1}]."
88+
)
89+
return False
7590
self.write_raw(f'cred del {sectag} {TLS_CRED_TYPES[cred_type]}')
7691
result, _ = self.serial_wait_for_response(
7792
"Deleted TLS credential", "There is no TLS credential"
@@ -94,25 +109,35 @@ def check_credential_exists(self, sectag, cred_type, get_hash=True):
94109
return True, None
95110

96111
data = output.decode().split(",")
97-
hash = data[2].strip()
112+
logger.debug(f"Cred list output: {data}")
113+
if len(data) < 4:
114+
logger.error("Invalid output format from device, skipping hash check.")
115+
return False, None
116+
cred_hash = data[2].strip()
98117
status_code = data[3].strip()
99118

100119
if status_code != "0":
101120
logger.warning(f"Error retrieving credential hash: {output.decode().strip()}.")
102121
logger.warning("Device might not support credential digests.")
103122
return True, None
104123

105-
return True, hash
124+
return True, cred_hash
106125

107126
def calculate_expected_hash(self, cred_text):
108-
hash = hashlib.sha256(cred_text.encode('utf-8') + b'\x00')
109-
return base64.b64encode(hash.digest()).decode()
127+
cred_hash = hashlib.sha256(cred_text.encode('utf-8') + b'\x00')
128+
return base64.b64encode(cred_hash.digest()).decode()
110129

111130
def check_cred_command(self):
112131
logger.info("Checking for 'cred' command existence...")
113132
self.serial_write_line("cred")
114-
result, output = self.serial_wait_for_response(timeout=5)
115-
if not result or (output and b"command not found" in output):
133+
result, output = self.serial_wait_for_response(
134+
"TLS Credentials Commands", "command not found", store="cred"
135+
)
136+
logger.debug(f"Result: {result}, Output: {output}")
137+
if not result:
138+
logger.error("Device did not respond to 'cred' command.")
139+
return False
140+
if output and b"command not found" in output:
116141
logger.error("Device does not support 'cred' command.")
117142
logger.error("Hint: Add 'CONFIG_TLS_CREDENTIALS_SHELL=y' to your prj.conf file.")
118143
return False
@@ -294,20 +319,25 @@ def main(in_args):
294319
logger.info(f'Deleting sectag {args.sectag}...')
295320
cred_if.delete_credential(args.sectag, args.cert_type)
296321

297-
cred_if.write_credential(args.sectag, args.cert_type, dev_bytes)
322+
result = cred_if.write_credential(args.sectag, args.cert_type, dev_bytes)
323+
if not result:
324+
logger.error(f'Failed to write credential for sectag {args.sectag}, it may already exist')
325+
sys.exit(5)
298326
logger.info(f'Writing sectag {args.sectag}...')
299-
result, hash = cred_if.check_credential_exists(args.sectag, args.cert_type, args.check_hash)
327+
result, cred_hash = cred_if.check_credential_exists(
328+
args.sectag, args.cert_type, args.check_hash
329+
)
300330
if args.check_hash:
301331
logger.debug(f'Checking hash for sectag {args.sectag}...')
302332
if not result:
303333
logger.error(f'Failed to check credential existence for sectag {args.sectag}')
304334
sys.exit(4)
305-
if hash:
306-
logger.debug(f'Credential hash: {hash}')
335+
if cred_hash:
336+
logger.debug(f'Credential hash: {cred_hash}')
307337
expected_hash = cred_if.calculate_expected_hash(dev_bytes)
308-
if hash != expected_hash:
338+
if cred_hash != expected_hash:
309339
logger.error(
310-
f'Hash mismatch for sectag {args.sectag}. Expected: {expected_hash}, got: {hash}'
340+
f'Hash mismatch for sectag {args.sectag}. Exp: {expected_hash}, got: {cred_hash}'
311341
)
312342
sys.exit(6)
313343
logger.info(f'Credential for sectag {args.sectag} written successfully')

0 commit comments

Comments
 (0)