Skip to content

Commit ebe66dc

Browse files
keys: fix signature generation
No need to create and pass an array with our template option. This was causing issues when we had multiple (>2) pairs of keys. Tests added to cover this scenario.
1 parent def1c6a commit ebe66dc

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/keys.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,11 @@ jwk_sign(const json_t* to_sign, const json_t* sig_keys)
233233
json_auto_t* sig_template = json_pack("{s:{s:s}}",
234234
"protected", "cty", "jwk-set+json");
235235

236-
/* Use the template with the signing keys. */
237-
json_auto_t* sig_template_arr = json_array();
238-
size_t arr_size = json_array_size(sig_keys);
239-
for (size_t i = 0; i < arr_size; i++) {
240-
if (json_array_append(sig_template_arr, sig_template) == -1) {
241-
fprintf(stderr, "Unable to append sig template to array\n");
242-
return NULL;
243-
}
244-
}
245-
246236
__attribute__ ((__cleanup__(cleanup_str))) char* data_to_sign = json_dumps(payload, 0);
247237
json_auto_t* jws = json_pack("{s:o}", "payload",
248238
jose_b64_enc(data_to_sign, strlen(data_to_sign)));
249239

250-
if (!jose_jws_sig(NULL, jws, sig_template_arr, sig_keys)) {
240+
if (!jose_jws_sig(NULL, jws, sig_template, sig_keys)) {
251241
fprintf(stderr, "Error trying to jose_jws_sign\n");
252242
return NULL;
253243
}

tests/adv

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ function on_exit() {
3131
[ -d "$TMP" ] && rm -rf $TMP
3232
}
3333

34+
validate() {
35+
if ! _jwks="$(jose fmt --json="${1}" -Og payload -SyOg keys \
36+
-AUo- 2>/dev/null)"; then
37+
echo "Advertisement is malformed" >&2
38+
exit 1
39+
fi
40+
_ver="$(printf '%s' "${_jwks}" | jose jwk use -i- -r -u verify -o-)"
41+
if ! printf '%s' "${_ver}" | jose jws ver -i "${1}" -k- -a; then
42+
echo "Advertisement is missing signatures" >&2
43+
exit 1
44+
fi
45+
}
46+
3447
trap 'on_exit' EXIT
3548
trap 'exit' ERR
3649

@@ -96,3 +109,18 @@ for k in *.jwk; do
96109
done
97110
cd -
98111
fetch /adv
112+
113+
# Lets's now test with multiple pairs of keys.
114+
for i in 1 2 3 4 5 6 7 8 9; do
115+
tangd-keygen "${TMP}"/db other-sig-${i} other-exc-${i}
116+
done
117+
118+
# Verify the advertisement is correct.
119+
validate "$(fetch /adv)"
120+
121+
# And make sure we can fetch an adv by its thumbprint.
122+
for jwk in "${TMP}"/db/other-sig-*.jwk; do
123+
for alg in $(jose alg -k hash); do
124+
fetch /adv/"$(jose jwk thp -a "${alg}" -i "${jwk}")" | ver "${jwk}"
125+
done
126+
done

0 commit comments

Comments
 (0)