Skip to content

Commit 6b9f5c0

Browse files
committed
nvme: add support to add derive TLS PSK to keyfile
When creating a new key and it is inserted into keystore also support to append it to a keyfile. Signed-off-by: Daniel Wagner <dwagner@suse.de>
1 parent 38dcd6f commit 6b9f5c0

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

nvme.c

+80
Original file line numberDiff line numberDiff line change
@@ -9175,6 +9175,75 @@ static int check_dhchap_key(int argc, char **argv, struct command *command, stru
91759175
return 0;
91769176
}
91779177

9178+
static int append_keyfile(const char *keyring, long id, const char *keyfile)
9179+
{
9180+
_cleanup_free_ unsigned char *key_data = NULL;
9181+
_cleanup_free_ char *exported_key = NULL;
9182+
_cleanup_free_ char *identity = NULL;
9183+
_cleanup_file_ FILE *fd = NULL;
9184+
int err, ver, hmac, key_len;
9185+
mode_t old_umask;
9186+
long kr_id;
9187+
char type;
9188+
9189+
kr_id = nvme_lookup_keyring(keyring);
9190+
if (kr_id <= 0) {
9191+
nvme_show_error("Failed to lookup keyring '%s', %s",
9192+
keyring, strerror(errno));
9193+
return -errno;
9194+
}
9195+
9196+
identity = nvme_describe_key_serial(id);
9197+
if (!identity) {
9198+
nvme_show_error("Failed to get identity info, %s",
9199+
strerror(errno));
9200+
return -errno;
9201+
}
9202+
9203+
if (sscanf(identity, "NVMe%01d%c%02d %*s", &ver, &type, &hmac) != 3) {
9204+
nvme_show_error("Failed to parse identity\n");
9205+
return -EINVAL;
9206+
}
9207+
9208+
key_data = nvme_read_key(kr_id, id, &key_len);
9209+
if (!key_data) {
9210+
nvme_show_error("Failed to read back derive TLS PSK, %s",
9211+
strerror(errno));
9212+
return -errno;
9213+
}
9214+
9215+
exported_key = nvme_export_tls_key_versioned(ver, hmac,
9216+
key_data, key_len);
9217+
if (!exported_key) {
9218+
nvme_show_error("Failed to export key, %s",
9219+
strerror(errno));
9220+
return -errno;
9221+
}
9222+
9223+
old_umask = umask(0);
9224+
9225+
fd = fopen(keyfile, "a");
9226+
if (!fd) {
9227+
nvme_show_error("Failed to open '%s', %s",
9228+
keyfile, strerror(errno));
9229+
err = -errno;
9230+
goto out;
9231+
}
9232+
9233+
err = fprintf(fd, "%s %s\n", identity, exported_key);
9234+
if (err < 0) {
9235+
nvme_show_error("Failed to append key to '%', %s",
9236+
keyfile, strerror(errno));
9237+
err = -errno;
9238+
}
9239+
9240+
out:
9241+
chmod(keyfile, 0600);
9242+
umask(old_umask);
9243+
9244+
return err;
9245+
}
9246+
91789247
static int gen_tls_key(int argc, char **argv, struct command *command, struct plugin *plugin)
91799248
{
91809249
const char *desc = "Generate a TLS key in NVMe PSK Interchange format.";
@@ -9187,6 +9256,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
91879256
const char *keyring = "Keyring for the retained key.";
91889257
const char *keytype = "Key type of the retained key.";
91899258
const char *insert = "Insert retained key into the keyring.";
9259+
const char *keyfile = "Update key file with the derive TLS PSK.";
91909260

91919261
_cleanup_free_ unsigned char *raw_secret = NULL;
91929262
_cleanup_free_ char *encoded_key = NULL;
@@ -9201,6 +9271,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
92019271
char *hostnqn;
92029272
char *subsysnqn;
92039273
char *secret;
9274+
char *keyfile;
92049275
unsigned char hmac;
92059276
unsigned char version;
92069277
bool insert;
@@ -9212,6 +9283,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
92129283
.hostnqn = NULL,
92139284
.subsysnqn = NULL,
92149285
.secret = NULL,
9286+
.keyfile = NULL,
92159287
.hmac = 1,
92169288
.version = 0,
92179289
.insert = false,
@@ -9223,6 +9295,7 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
92239295
OPT_STR("hostnqn", 'n', &cfg.hostnqn, hostnqn),
92249296
OPT_STR("subsysnqn", 'c', &cfg.subsysnqn, subsysnqn),
92259297
OPT_STR("secret", 's', &cfg.secret, secret),
9298+
OPT_STR("keyfile", 'f', &cfg.keyfile, keyfile),
92269299
OPT_BYTE("hmac", 'm', &cfg.hmac, hmac),
92279300
OPT_BYTE("identity", 'I', &cfg.version, version),
92289301
OPT_FLAG("insert", 'i', &cfg.insert, insert));
@@ -9296,7 +9369,14 @@ static int gen_tls_key(int argc, char **argv, struct command *command, struct pl
92969369
}
92979370

92989371
printf("Inserted TLS key %08x\n", (unsigned int)tls_key);
9372+
9373+
if (cfg.keyfile) {
9374+
err = append_keyfile(cfg.keyring, tls_key, cfg.keyfile);
9375+
if (err)
9376+
return err;
9377+
}
92999378
}
9379+
93009380
return 0;
93019381
}
93029382

0 commit comments

Comments
 (0)