Skip to content

Commit 6e8e03a

Browse files
dwsuseigaw
authored andcommitted
linux: export keys to config
The recent change where key import function out of the JSON parser to the connect path, dropped the feature to export the keys back to the config. The keys are only necessary to be loaded back to the user memory when the configuration is dumped. Thus hook the export functionality into the dump code path. Signed-off-by: Daniel Wagner <[email protected]>
1 parent b9f08d5 commit 6e8e03a

3 files changed

Lines changed: 94 additions & 2 deletions

File tree

src/nvme/linux.c

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,79 @@ int __nvme_import_keys_from_config(nvme_host_t h, nvme_ctrl_t c,
14811481

14821482
return 0;
14831483
}
1484+
1485+
static char *__nvme_export_key(long keyring, long key_id, char **identity)
1486+
{
1487+
_cleanup_free_ unsigned char *key = NULL;
1488+
int len, ver, hmac;
1489+
char type, *desc, *encoded_key;
1490+
1491+
key = nvme_read_key(keyring, key_id, &len);
1492+
if (!key) {
1493+
/*
1494+
* Accessing the keyring is a priveleged opartion, thus it
1495+
* might fail for a normal user, this is not an error.
1496+
*/
1497+
return NULL;
1498+
}
1499+
1500+
desc = nvme_describe_key_serial(key_id);
1501+
if (!desc) {
1502+
/*
1503+
* Revoked keys don't return a description, thus ignore
1504+
* them.
1505+
*/
1506+
return NULL;
1507+
}
1508+
1509+
if (sscanf(desc, "NVMe%01d%c%02d %*s", &ver, &type, &hmac) != 3)
1510+
return NULL;
1511+
1512+
encoded_key = nvme_export_tls_key_versioned(ver, hmac, key, len);
1513+
if (!encoded_key)
1514+
return NULL;
1515+
1516+
if (identity)
1517+
*identity = desc;
1518+
return encoded_key;
1519+
}
1520+
1521+
static void export_keys_to_config(nvme_ctrl_t c)
1522+
{
1523+
char *identity = NULL, *encoded_key;
1524+
1525+
if (!c->cfg.tls)
1526+
return;
1527+
/*
1528+
* Do not update the configuration blindly. The user could have
1529+
* provided configuration, but they keys are not loaded into
1530+
* keystore yet.
1531+
*/
1532+
1533+
encoded_key =
1534+
__nvme_export_key(c->cfg.keyring, c->cfg.tls_key, &identity);
1535+
if (identity) {
1536+
nvme_ctrl_set_tls_key_identity(c, identity);
1537+
free(identity);
1538+
}
1539+
if (encoded_key) {
1540+
nvme_ctrl_set_tls_key(c, encoded_key);
1541+
free(encoded_key);
1542+
}
1543+
}
1544+
1545+
int __nvme_export_keys_to_config(nvme_root_t r)
1546+
{
1547+
nvme_host_t h;
1548+
nvme_subsystem_t s;
1549+
nvme_ctrl_t c;
1550+
1551+
nvme_for_each_host(r, h)
1552+
nvme_for_each_subsystem(h, s)
1553+
nvme_subsystem_for_each_ctrl(s, c)
1554+
export_keys_to_config(c);
1555+
return 0;
1556+
}
14841557
#else
14851558
long nvme_lookup_keyring(const char *keyring)
14861559
{
@@ -1558,8 +1631,11 @@ long nvme_revoke_tls_key(const char *keyring, const char *key_type,
15581631
int __nvme_import_keys_from_config(nvme_host_t h, nvme_ctrl_t c,
15591632
long *keyring_id, long *key_id)
15601633
{
1561-
nvme_msg(h->r, LOG_ERR, "key operations not supported; "
1562-
"recompile with keyutils support.\n");
1634+
return -ENOTSUP;
1635+
}
1636+
1637+
int __nvme_export_keys_to_config(nvme_root_t r)
1638+
{
15631639
return -ENOTSUP;
15641640
}
15651641
#endif

src/nvme/private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,6 @@ void __nvme_mi_mctp_set_ops(const struct __mi_mctp_socket_ops *newops);
302302

303303
int __nvme_import_keys_from_config(nvme_host_t h, nvme_ctrl_t c,
304304
long *keyring_id, long *key_id);
305+
int __nvme_export_keys_to_config(nvme_root_t r);
306+
305307
#endif /* _LIBNVME_PRIVATE_H */

src/nvme/tree.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,20 @@ int nvme_update_config(nvme_root_t r)
346346

347347
int nvme_dump_config(nvme_root_t r)
348348
{
349+
int err;
350+
351+
err = __nvme_export_keys_to_config(r);
352+
if (err) {
353+
if (err == -ENOTSUP) {
354+
nvme_msg(r, LOG_NOTICE,
355+
"exporting keys to the configuration failed because keysutils is missing\n");
356+
} else {
357+
nvme_msg(r, LOG_ERR,
358+
"exporting keys to the configuration failed with %s\n",
359+
nvme_errno_to_string(err));
360+
}
361+
}
362+
349363
return json_update_config(r, NULL);
350364
}
351365

0 commit comments

Comments
 (0)