Skip to content

Commit

Permalink
libnvme: reshuffle nvme_generate_tls_key_identity()
Browse files Browse the repository at this point in the history
Reshuffle nvme_generate_tls_key_identity and move it out of the
'#ifdef CONFIG_KEYUTILS' section to avoid build failures.

Signed-off-by: Hannes Reinecke <hare@suse.de>
  • Loading branch information
hreinecke committed Nov 20, 2023
1 parent e407735 commit b43d921
Showing 1 changed file with 35 additions and 44 deletions.
79 changes: 35 additions & 44 deletions src/nvme/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,6 @@ static int gen_tls_identity(const char *hostnqn, const char *subsysnqn,
}
#endif /* !CONFIG_OPENSSL_3 */

#ifdef CONFIG_KEYUTILS
static int derive_nvme_keys(const char *hostnqn, const char *subsysnqn,
char *identity, int version,
int hmac, unsigned char *configured,
Expand All @@ -1101,7 +1100,7 @@ static int derive_nvme_keys(const char *hostnqn, const char *subsysnqn,
unsigned char *retained;
int ret = -1;

if (!hostnqn || !subsysnqn || !identity) {
if (!hostnqn || !subsysnqn || !identity || !psk) {
errno = EINVAL;
return -1;
}
Expand Down Expand Up @@ -1141,6 +1140,40 @@ static size_t nvme_identity_len(int hmac, int version, const char *hostnqn,
return len;
}

char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn,
int version, int hmac,
unsigned char *configured_key, int key_len)
{
char *identity;
size_t identity_len;
unsigned char *psk;
int ret = -1;

identity_len = nvme_identity_len(hmac, version, hostnqn, subsysnqn);
if (identity_len < 0)
return NULL;

identity = malloc(identity_len);
if (!identity)
return NULL;

psk = malloc(key_len);
if (!psk)
goto out_free_identity;

memset(psk, 0, key_len);
ret = derive_nvme_keys(hostnqn, subsysnqn, identity, version, hmac,
configured_key, psk, key_len);
free(psk);
out_free_identity:
if (ret < 0) {
free(identity);
identity = NULL;
}
return identity;
}

#ifdef CONFIG_KEYUTILS
long nvme_lookup_keyring(const char *keyring)
{
key_serial_t keyring_id;
Expand Down Expand Up @@ -1233,38 +1266,6 @@ long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type,
return key;
}

char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn,
int version, int hmac,
unsigned char *configured_key, int key_len)
{
char *identity;
size_t identity_len;
unsigned char *psk;
int ret = -1;

identity_len = nvme_identity_len(hmac, version, hostnqn, subsysnqn);
if (identity_len < 0)
return NULL;

identity = malloc(identity_len);
if (!identity)
return NULL;

psk = malloc(key_len);
if (!psk)
goto out_free_identity;

memset(psk, 0, key_len);
ret = derive_nvme_keys(hostnqn, subsysnqn, identity, version, hmac,
configured_key, psk, key_len);
free(psk);
out_free_identity:
if (ret < 0) {
free(identity);
identity = NULL;
}
return identity;
}
#else
long nvme_lookup_keyring(const char *keyring)
{
Expand Down Expand Up @@ -1308,16 +1309,6 @@ long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type,
errno = ENOTSUP;
return -1;
}

char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn,
int version, int hmac,
unsigned char *configured_key, int key_len)
{
nvme_msg(NULL, LOG_ERR, "key operations not supported; "
"recompile with keyutils support.\n");
errno = ENOTSUP;
return -1;
}
#endif

long nvme_insert_tls_key(const char *keyring, const char *key_type,
Expand Down

0 comments on commit b43d921

Please sign in to comment.