Skip to content

Commit f865852

Browse files
authored
Merge pull request #21560 from benpicco/suit_get_public_key
sys/suit: add suit_get_public_key()
2 parents dd32b0a + 8bbe166 commit f865852

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

sys/include/suit.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,17 @@ static inline bool suit_component_check_flag(suit_component_t *component,
325325
int suit_component_name_to_string(const suit_manifest_t *manifest,
326326
const suit_component_t *component,
327327
char separator, char *buf, size_t buf_len);
328+
329+
/**
330+
* @brief Get public key accepted by SUIT
331+
*
332+
* @param[in] idx Index of the key to query
333+
* @param[out] key The public key used to verify the signature
334+
*
335+
* @returns True if a key at that index exists
336+
*/
337+
bool suit_get_public_key(uint8_t idx, cose_key_t *key);
338+
328339
#ifdef __cplusplus
329340
}
330341
#endif

sys/suit/handlers_envelope.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,21 @@
3232
#include "suit/handlers.h"
3333
#include "suit.h"
3434

35+
bool suit_get_public_key(uint8_t idx, cose_key_t *pkey)
36+
{
37+
if (idx >= ARRAY_SIZE(public_key)) {
38+
return false;
39+
}
40+
41+
/* Initialize key from hardcoded public key */
42+
cose_key_init(pkey);
43+
cose_key_set_keys(pkey, COSE_EC_CURVE_ED25519, COSE_ALGO_EDDSA,
44+
(void *)public_key[idx], NULL, NULL);
45+
return true;
46+
}
47+
3548
static int _verify_with_key(suit_manifest_t *manifest, const nanocbor_value_t *it,
36-
const void *key)
49+
cose_key_t *pkey)
3750
{
3851
cose_sign_dec_t verify;
3952
const uint8_t *cose_buf;
@@ -48,12 +61,6 @@ static int _verify_with_key(suit_manifest_t *manifest, const nanocbor_value_t *i
4861
return SUIT_ERR_INVALID_MANIFEST;
4962
}
5063

51-
/* Initialize key from hardcoded public key */
52-
cose_key_t pkey;
53-
cose_key_init(&pkey);
54-
cose_key_set_keys(&pkey, COSE_EC_CURVE_ED25519, COSE_ALGO_EDDSA,
55-
(void *)key, NULL, NULL);
56-
5764
nanocbor_value_t _cont, arr;
5865
nanocbor_decoder_init(&_cont, auth_container, auth_container_len);
5966

@@ -87,7 +94,7 @@ static int _verify_with_key(suit_manifest_t *manifest, const nanocbor_value_t *i
8794
}
8895
LOG_INFO("suit: verifying manifest signature\n");
8996
int verification = cose_sign_verify(&verify, &signature,
90-
&pkey, manifest->validation_buf,
97+
pkey, manifest->validation_buf,
9198
SUIT_COSE_BUF_SIZE);
9299
if (verification == 0) {
93100
manifest->state |= SUIT_STATE_COSE_AUTHENTICATED;
@@ -111,9 +118,11 @@ static int _auth_handler(suit_manifest_t *manifest, int key,
111118
(void)key;
112119

113120
int res = 0;
121+
unsigned idx = 0;
122+
cose_key_t pkey;
114123

115-
for (unsigned i = 0; i < ARRAY_SIZE(public_key); ++i) {
116-
res = _verify_with_key(manifest, it, public_key[i]);
124+
while (suit_get_public_key(idx++, &pkey)) {
125+
res = _verify_with_key(manifest, it, &pkey);
117126
if (res != SUIT_ERR_SIGNATURE) {
118127
break;
119128
}

0 commit comments

Comments
 (0)