Skip to content

Commit

Permalink
lib: modem_attest_token: create lower-case v4 formatted UUID strings
Browse files Browse the repository at this point in the history
Create lower-case UUID v4 formatted strings instead of
upper-case with no hyphens.

Signed-off-by: Justin Morton <[email protected]>
  • Loading branch information
jayteemo authored and trond-snekvik committed Jun 18, 2021
1 parent e29b8ce commit 9aad999
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
17 changes: 10 additions & 7 deletions include/modem/modem_attest_token.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ enum nrf_device_type {
NRF_DEVICE_TYPE_9160_SICA = 3,
};

#define NRF_DEVICE_UUID_SZ 16
#define NRF_MODEM_FW_UUID_SZ 16
#define NRF_ATTEST_NONCE_SZ 16
#define NRF_UUID_BYTE_SZ 16
#define NRF_DEVICE_UUID_SZ NRF_UUID_BYTE_SZ
#define NRF_MODEM_FW_UUID_SZ NRF_UUID_BYTE_SZ
#define NRF_ATTEST_NONCE_SZ 16

#define NRF_DEVICE_UUID_STR_LEN (NRF_DEVICE_UUID_SZ * 2)
#define NRF_MODEM_FW_UUID_STR_LEN (NRF_MODEM_FW_UUID_SZ * 2)
/* UUID v4 format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
#define NRF_UUID_V4_STR_LEN ((NRF_UUID_BYTE_SZ * 2) + 4)
#define NRF_DEVICE_UUID_STR_LEN NRF_UUID_V4_STR_LEN
#define NRF_MODEM_FW_UUID_STR_LEN NRF_UUID_V4_STR_LEN

/** @brief Parsed attestation token data */
struct nrf_attestation_data {
Expand All @@ -66,12 +69,12 @@ struct nrf_attestation_data {
char nonce[NRF_ATTEST_NONCE_SZ];
};

/** @brief Device UUID string (no hyphens) */
/** @brief Device UUID v4 string */
struct nrf_device_uuid {
char str[NRF_DEVICE_UUID_STR_LEN + 1];
};

/** @brief Modem firmware UUID string (no hyphens) */
/** @brief Modem firmware UUID v4 string */
struct nrf_modem_fw_uuid {
char str[NRF_MODEM_FW_UUID_STR_LEN + 1];
};
Expand Down
51 changes: 41 additions & 10 deletions lib/modem_attest_token/modem_attest_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
*/
#define ATTEST_MAP_LEN 5

/* UUID v4 hypen locations with respect to the original byte array */
#define UUID_HYPHEN_POS_1 (8 / 2)
#define UUID_HYPHEN_POS_2 (12 / 2)
#define UUID_HYPHEN_POS_3 (16 / 2)
#define UUID_HYPHEN_POS_4 (20 / 2)

#define ATTEST_TOKEN_RESP_LEN 200
BUILD_ASSERT(
ATTEST_TOKEN_RESP_LEN <= CONFIG_AT_CMD_RESPONSE_MAX_LEN,
Expand Down Expand Up @@ -448,6 +454,33 @@ int modem_attest_token_parse(struct nrf_attestation_token const *const token_in,
return err;
}

static int format_uuid(const char * const bytes_in, const size_t bytes_sz,
char * const str_out, const size_t str_sz)
{
__ASSERT_NO_MSG(bytes_in != NULL);
__ASSERT_NO_MSG(bytes_sz == NRF_UUID_BYTE_SZ);
__ASSERT_NO_MSG(str_out != NULL);
__ASSERT_NO_MSG(str_sz >= NRF_UUID_V4_STR_LEN);

int sz;
size_t w_pos = 0;

for (int i = 0; (i < bytes_sz); ++i) {
if ((i == UUID_HYPHEN_POS_1) || (i == UUID_HYPHEN_POS_2) ||
(i == UUID_HYPHEN_POS_3) || (i == UUID_HYPHEN_POS_4)) {
str_out[w_pos++] = '-';
}

sz = sprintf(&str_out[w_pos], "%02x", bytes_in[i]);
if (sz != 2) {
return -EIO;
}
w_pos += 2;
}

return 0;
}

int modem_attest_token_get_uuids(struct nrf_device_uuid *dev,
struct nrf_modem_fw_uuid *mfw)
{
Expand All @@ -471,20 +504,18 @@ int modem_attest_token_get_uuids(struct nrf_device_uuid *dev,
goto cleanup;
}

for (int i = 0; (dev) && (i < NRF_DEVICE_UUID_SZ); ++i) {
int sz = sprintf(&dev->str[i * 2], "%02X",
a_data.device_uuid[i]);
if (sz != 2) {
err = -EIO;
if (dev) {
err = format_uuid(a_data.device_uuid, sizeof(a_data.device_uuid),
dev->str, sizeof(dev->str));
if (err) {
goto cleanup;
}
}

for (int i = 0; (mfw) && (i < NRF_MODEM_FW_UUID_SZ); ++i) {
int sz = sprintf(&mfw->str[i * 2], "%02X",
a_data.fw_uuid[i]);
if (sz != 2) {
err = -EIO;
if (mfw) {
err = format_uuid(a_data.fw_uuid, sizeof(a_data.fw_uuid),
mfw->str, sizeof(mfw->str));
if (err) {
goto cleanup;
}
}
Expand Down

0 comments on commit 9aad999

Please sign in to comment.