Retrieving the cert for the last key of a token fails due to an
off-by-one bug in find_privkeys():
In the loop that iterates the keys, "current" contains the index
of the "next" key slot, which is also the active "count" of populated
slots in the output struct find_pkey_list_st.
The current statement:
list->key_ids_size = current - 1;
Means we return a "key_ids_size" of the current count minus one, ie 0
for 1 key etc... However, this isn't what the callers expect, for example:
find_multi_objs_cb() does:
ret = find_privkeys(sinfo, tinfo, &plist);
if (ret < 0) {
gnutls_assert();
return ret;
}
if (plist.key_ids_size == 0) {
gnutls_assert();
return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
}
So a slot with a single key will fail when trying to find a certificate
Subsequent uses of "plist" in that function also show that it's expected
to contain the real slot count:
for (i = 0; i < plist.key_ids_size; i++) {
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
pkcs11_find_objects_final(sinfo);
- list->key_ids_size = current - 1;
+ list->key_ids_size = current;
return 0;
}