diff options
Diffstat (limited to 'projects/cli-test')
-rw-r--r-- | projects/cli-test/mgmt-keystore.c | 115 |
1 files changed, 79 insertions, 36 deletions
diff --git a/projects/cli-test/mgmt-keystore.c b/projects/cli-test/mgmt-keystore.c index 1fb720b..3afd238 100644 --- a/projects/cli-test/mgmt-keystore.c +++ b/projects/cli-test/mgmt-keystore.c @@ -231,53 +231,96 @@ static int cmd_keystore_show_data(struct cli_def *cli, const char *command, char return CLI_OK; } -static int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[], int argc) +static int show_keys(struct cli_def *cli, const char *title, const hal_key_flags_t qflags) { - hal_pkey_info_t keys[64]; - unsigned n; + const hal_client_handle_t client = { -1 }; + const hal_session_handle_t session = { HAL_HANDLE_NONE }; + char key_name[HAL_UUID_TEXT_SIZE]; + hal_uuid_t previous_uuid = {{0}}; + hal_pkey_handle_t pkey; + hal_curve_name_t curve; + hal_key_flags_t flags; + hal_key_type_t type; hal_error_t status; - hal_client_handle_t client = {HAL_HANDLE_NONE}; - hal_session_handle_t session = {HAL_HANDLE_NONE}; - - if ((status = hal_rpc_pkey_list(client, session, keys, &n, sizeof(keys)/sizeof(*keys), - HAL_KEY_FLAG_TOKEN)) != LIBHAL_OK) { - cli_print(cli, "Could not fetch key info: %s", hal_error_string(status)); - return CLI_ERROR; - } + hal_uuid_t uuids[50]; + unsigned n; + int done = 0; - for (int i = 0; i < n; i++) { - char name[HAL_UUID_TEXT_SIZE]; - const char *type, *curve; + cli_print(cli, title); - switch (keys[i].type) { - case HAL_KEY_TYPE_RSA_PRIVATE: type = "RSA private"; break; - case HAL_KEY_TYPE_RSA_PUBLIC: type = "RSA public"; break; - case HAL_KEY_TYPE_EC_PRIVATE: type = "EC private"; break; - case HAL_KEY_TYPE_EC_PUBLIC: type = "EC public"; break; - default: type = "unknown"; break; - } + while (!done) { - switch (keys[i].curve) { - case HAL_CURVE_NONE: curve = "none"; break; - case HAL_CURVE_P256: curve = "P-256"; break; - case HAL_CURVE_P384: curve = "P-384"; break; - case HAL_CURVE_P521: curve = "P-521"; break; - default: curve = "unknown"; break; + if ((status = hal_rpc_pkey_match(client, session, HAL_KEY_TYPE_NONE, HAL_CURVE_NONE, + qflags, NULL, 0, uuids, &n, sizeof(uuids)/sizeof(*uuids), + &previous_uuid)) != LIBHAL_OK) { + cli_print(cli, "Could not fetch UUID list: %s", hal_error_string(status)); + return 0; } - if ((status = hal_uuid_format(&keys[i].name, name, sizeof(name))) != LIBHAL_OK) { - cli_print(cli, "Could not convert key name: %s", hal_error_string(status)); - return CLI_ERROR; + done = n < sizeof(uuids)/sizeof(*uuids); + + if (!done) + previous_uuid = uuids[sizeof(uuids)/sizeof(*uuids) - 1]; + + for (int i = 0; i < n; i++) { + + if ((status = hal_uuid_format(&uuids[i], key_name, sizeof(key_name))) != LIBHAL_OK) { + cli_print(cli, "Could not convert key name: %s", + hal_error_string(status)); + return 0; + } + + if ((status = hal_rpc_pkey_open(client, session, &pkey, &uuids[i], qflags)) != LIBHAL_OK) { + cli_print(cli, "Could not open key %s: %s", + key_name, hal_error_string(status)); + return 0; + } + + if ((status = hal_rpc_pkey_get_key_type(pkey, &type)) != LIBHAL_OK || + (status = hal_rpc_pkey_get_key_curve(pkey, &curve)) != LIBHAL_OK || + (status = hal_rpc_pkey_get_key_flags(pkey, &flags)) != LIBHAL_OK) + cli_print(cli, "Could not fetch metadata for key %s: %s", + key_name, hal_error_string(status)); + + if (status == LIBHAL_OK) + status = hal_rpc_pkey_close(pkey); + else + (void) hal_rpc_pkey_close(pkey); + + if (status != LIBHAL_OK) + return 0; + + const char *type_name = "unknown"; + switch (type) { + case HAL_KEY_TYPE_NONE: type_name = "none"; break; + case HAL_KEY_TYPE_RSA_PRIVATE: type_name = "RSA private"; break; + case HAL_KEY_TYPE_RSA_PUBLIC: type_name = "RSA public"; break; + case HAL_KEY_TYPE_EC_PRIVATE: type_name = "EC private"; break; + case HAL_KEY_TYPE_EC_PUBLIC: type_name = "EC public"; break; + } + + const char *curve_name = "unknown"; + switch (curve) { + case HAL_CURVE_NONE: curve_name = "none"; break; + case HAL_CURVE_P256: curve_name = "P-256"; break; + case HAL_CURVE_P384: curve_name = "P-384"; break; + case HAL_CURVE_P521: curve_name = "P-521"; break; + } + + cli_print(cli, "Key %2i, name %s, type %s, curve %s, flags 0x%lx", + i, key_name, type_name, curve_name, (unsigned long) flags); } - - cli_print(cli, "Key %2i, name %s, type %s, curve %s, flags 0x%lx", - i, name, type, curve, (unsigned long) keys[i].flags); - } - cli_print(cli, "\n"); + return 1; +} - return CLI_OK; +static int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[], int argc) +{ + int ok = 1; + ok &= show_keys(cli, "Memory keystore:", 0); + ok &= show_keys(cli, "Token keystore:", HAL_KEY_FLAG_TOKEN); + return ok ? CLI_OK : CLI_ERROR; } static int cmd_keystore_erase(struct cli_def *cli, const char *command, char *argv[], int argc) |