Skip to content

Commit d027f68

Browse files
hreineckeigaw
authored andcommitted
json: import TLS key from PSK interchange format
As now the JSON configuration file holds the TLS key in PSK interchange format we should be parsing that key and inserting it into the kernel keystore to make it available for TLS. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent d46c0fe commit d027f68

1 file changed

Lines changed: 47 additions & 9 deletions

File tree

src/nvme/json.c

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,40 @@
2525
#define JSON_UPDATE_BOOL_OPTION(c, k, a, o) \
2626
if (!strcmp(# a, k ) && !c->a) c->a = json_object_get_boolean(o);
2727

28+
static void json_import_nvme_tls_key(nvme_ctrl_t c, const char *keyring_str,
29+
const char *encoded_key)
30+
{
31+
struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
32+
const char *hostnqn = nvme_host_get_hostnqn(c->s->h);
33+
const char *subsysnqn = nvme_ctrl_get_subsysnqn(c);
34+
int key_len;
35+
unsigned int hmac;
36+
long key_id;
37+
_cleanup_free_ unsigned char *key_data;
38+
39+
if (!hostnqn || !subsysnqn) {
40+
nvme_msg(NULL, LOG_ERR, "Invalid NQNs (%s, %s)\n",
41+
hostnqn, subsysnqn);
42+
return;
43+
}
44+
key_data = nvme_import_tls_key(encoded_key, &key_len, &hmac);
45+
if (!key_data) {
46+
nvme_msg(NULL, LOG_ERR, "Failed to decode TLS Key '%s'\n",
47+
encoded_key);
48+
return;
49+
}
50+
key_id = nvme_insert_tls_key_versioned(keyring_str, "psk",
51+
hostnqn, subsysnqn,
52+
0, hmac, key_data, key_len);
53+
if (key_id <= 0)
54+
nvme_msg(NULL, LOG_ERR, "Failed to insert TLS KEY, error %d\n",
55+
errno);
56+
else {
57+
cfg->tls_key = key_id;
58+
cfg->tls = true;
59+
}
60+
}
61+
2862
static void json_export_nvme_tls_key(long keyring_id, long tls_key,
2963
struct json_object *obj)
3064
{
@@ -46,6 +80,7 @@ static void json_update_attributes(nvme_ctrl_t c,
4680
struct json_object *ctrl_obj)
4781
{
4882
struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
83+
const char *keyring_str = NULL, *encoded_key = NULL;
4984

5085
json_object_object_foreach(ctrl_obj, key_str, val_obj) {
5186
JSON_UPDATE_INT_OPTION(cfg, key_str,
@@ -92,21 +127,24 @@ static void json_update_attributes(nvme_ctrl_t c,
92127
if (!strcmp("keyring", key_str) && cfg->keyring == 0) {
93128
long keyring;
94129

95-
keyring = nvme_lookup_keyring(json_object_get_string(val_obj));
130+
keyring_str = json_object_get_string(val_obj);
131+
keyring = nvme_lookup_keyring(keyring_str);
96132
if (keyring) {
97133
cfg->keyring = keyring;
98134
nvme_set_keyring(cfg->keyring);
99135
}
100136
}
101-
if (!strcmp("tls_key", key_str) && cfg->tls_key == 0) {
102-
long key;
103-
104-
key = nvme_lookup_key("psk",
105-
json_object_get_string(val_obj));
106-
if (key)
107-
cfg->tls_key = key;
108-
}
137+
if (!strcmp("tls_key", key_str) && cfg->tls_key == 0)
138+
encoded_key = json_object_get_string(val_obj);
109139
}
140+
141+
/*
142+
* We might need the keyring information from the above loop,
143+
* so we can only import the TLS key once all entries are
144+
* processed.
145+
*/
146+
if (encoded_key)
147+
json_import_nvme_tls_key(c, keyring_str, encoded_key);
110148
}
111149

112150
static void json_parse_port(nvme_subsystem_t s, struct json_object *port_obj)

0 commit comments

Comments
 (0)