Skip to content

Commit b1989aa

Browse files
committed
add back get_csr
1 parent e1bba7d commit b1989aa

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

command_interface.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,34 @@ def get_attestation_token(self):
177177
attest_tok = output.split('"')[1]
178178
return attest_tok
179179

180+
def get_csr(self, sectag=0, cn=""):
181+
"""Ask a device with modem to generate CSR using AT%KEYGEN.
182+
183+
Returns:
184+
x509.CertificateSigningRequest object.
185+
"""
186+
187+
# provide attributes parameter if a custom CN is specified
188+
attr = f',"CN={cn}"' if len(cn) else ''
189+
190+
self.at_command(f'AT%KEYGEN={sectag},2,0{attr}')
191+
192+
# include the CR in OK because 'OK' could be found in the CSR string
193+
retval, output = self.comms.expect_response("OK", "ERROR", "%KEYGEN:")
194+
195+
if not retval:
196+
return None
197+
198+
# convert the encoded blob to an actual cert
199+
csr_blob = str(output).split('"')[1]
200+
logger.debug('CSR blob: {}'.format(csr_blob))
201+
202+
# format is "body.cose"
203+
# body is base64 encoded DER
204+
# cose is base64 encoded COSE header (CBOR)
205+
206+
return csr_blob
207+
180208
TLS_CRED_TYPES = ["CA", "SERV", "PK"]
181209
# This chunk size can be any multiple of 4, as long as it is small enough to fit within the
182210
# Zephyr shell buffer.

0 commit comments

Comments
 (0)