Skip to content

Commit def1c6a

Browse files
Fix generation of new keys when no keys are available
When no keys are available, tang creates a new pair of keys, however currently it checks the total number of keys, including rotated keys, to decide whether to create new keys. So not to have issues when all the keys have been rotated, let's check instead the total number of "regular" keys, the ones that will be advertised, and if there are none, then tang can create new keys. This fixes an issue when we do have all keys rotated. Tests added as well.
1 parent d98ce92 commit def1c6a

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/keys.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,15 @@ load_keys(const char* jwkdir)
392392
json_t* arr = tki->m_keys;
393393
if (d->d_name[0] == '.') {
394394
arr = tki->m_rotated_keys;
395+
tki->m_rotated_keys_count++;
396+
} else {
397+
tki->m_keys_count++;
395398
}
399+
396400
if (json_array_append(arr, json) == -1) {
397401
fprintf(stderr, "Unable to append JSON (%s) to array; skipping\n", d->d_name);
398402
continue;
399403
}
400-
tki->m_keys_count++;
401404
}
402405
}
403406
closedir(dir);

src/keys.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ struct tang_keys_info {
3434
json_t* m_sign; /* Set of signing keys made from regular
3535
keys. */
3636

37-
size_t m_keys_count; /* Number of keys (regular + rotated). */
38-
37+
size_t m_keys_count; /* Number of regular keys. */
38+
size_t m_rotated_keys_count; /* Number of rotated keys. */
3939
};
4040

4141
void cleanup_tang_keys_info(struct tang_keys_info**);

tests/adv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,15 @@ fetch /adv/`jose jwk thp -i $TMP/db/.sig.jwk` \
8484

8585
THP_DEFAULT_HASH=S256 # SHA-256.
8686
test "$(tang-show-keys $PORT)" == "$(jose jwk thp -a "${THP_DEFAULT_HASH}" -i $TMP/db/sig.jwk)"
87+
88+
# Check that new keys will be created if none exist.
89+
rm -rf "${TMP}/db" && mkdir -p "${TMP}/db"
90+
fetch /adv
91+
92+
# Now let's rotate these keys and check if we still create new keys.
93+
cd "${TMP}/db"
94+
for k in *.jwk; do
95+
mv -f -- "${k}" ".${k}"
96+
done
97+
cd -
98+
fetch /adv

tests/test-keys.c.in

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ test_find_jws(void)
140140
json_auto_t* keys = json_deep_copy(tki->m_keys);
141141
ASSERT(keys);
142142
ASSERT(json_array_extend(keys, tki->m_rotated_keys) == 0);
143-
ASSERT(json_array_size(keys) == (size_t)tki->m_keys_count);
143+
ASSERT(json_array_size(keys) == (size_t)(tki->m_keys_count + tki->m_rotated_keys_count));
144144

145145
for (int i = 0; hashes[i]; i++) {
146146
json_array_foreach(keys, idx, jwk) {
@@ -203,7 +203,7 @@ test_find_jwk(void)
203203
json_auto_t* keys = json_deep_copy(tki->m_keys);
204204
ASSERT(keys);
205205
ASSERT(json_array_extend(keys, tki->m_rotated_keys) == 0);
206-
ASSERT(json_array_size(keys) == (size_t)tki->m_keys_count);
206+
ASSERT(json_array_size(keys) == (size_t)(tki->m_keys_count + tki->m_rotated_keys_count));
207207

208208
for (int i = 0; hashes[i]; i++) {
209209
json_array_foreach(keys, idx, jwk) {
@@ -230,7 +230,8 @@ test_read_keys(void)
230230
* - qgmqJSo6AEEuVQY7zVlklqdTMqY.jwk
231231
* - -bWkGaJi0Zdvxaj4DCp28umLcRA.jwk
232232
*/
233-
ASSERT(tki->m_keys_count == 4);
233+
ASSERT(tki->m_keys_count == 2);
234+
ASSERT(tki->m_rotated_keys_count == 2);
234235
ASSERT(json_array_size(tki->m_keys) == 2);
235236
ASSERT(json_array_size(tki->m_rotated_keys) == 2);
236237

0 commit comments

Comments
 (0)