Skip to content

Commit 10517fb

Browse files
committed
libnvme: separate out 'gen_tls_identity' and reshuffle 'derive_nvme_keys'
Separate out a function to generate the TLS identity; this allows us to reshuffle 'derive_nvme_keys()' to compile it only when KEYUTILS is selected. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent e2c716c commit 10517fb

1 file changed

Lines changed: 83 additions & 33 deletions

File tree

src/nvme/linux.c

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,33 @@ int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac,
542542
return 0;
543543
}
544544

545-
static int derive_nvme_keys(const char *hostnqn, const char *identity,
546-
int hmac, unsigned char *configured,
547-
unsigned char *psk, int key_len)
545+
static int derive_retained_key(int hmac, const char *hostnqn,
546+
unsigned char *generated,
547+
unsigned char *retained,
548+
size_t key_len)
549+
{
550+
nvme_msg(NULL, LOG_ERR, "NVMe TLS is not supported; "
551+
"recompile with OpenSSL support.\n");
552+
errno = NOTSUP;
553+
return -1;
554+
}
555+
556+
static int gen_tls_identity(const char *hostnqn, const char *subsysnqn,
557+
int hmac, char *identity,
558+
unsigned char *retained, size_t key_len)
548559
{
549-
errno = EOPNOTSUPP;
560+
sprintf(identity, "NVMe0R%02d %s %s",
561+
version, hmac, hostnqn, subsysnqn);
562+
return strlen(identity);
563+
}
564+
565+
static int derive_tls_key(int hmac, const char *identity,
566+
unsigned char *retained,
567+
unsigned char *psk, size_t key_len)
568+
{
569+
nvme_msg(NULL, LOG_ERR, "NVMe TLS is not supported; "
570+
"recompile with OpenSSL support.\n");
571+
errno = NOTSUP;
550572
return -1;
551573
}
552574
#else /* CONFIG_OPENSSL */
@@ -683,30 +705,6 @@ static int derive_tls_key(int hmac, const char *identity,
683705

684706
return ret;
685707
}
686-
687-
static int derive_nvme_keys(const char *hostnqn, const char *identity,
688-
int hmac, unsigned char *configured,
689-
unsigned char *psk, int key_len)
690-
{
691-
unsigned char *retained;
692-
int ret = -1;
693-
694-
if (!hostnqn || !identity) {
695-
errno = EINVAL;
696-
return -1;
697-
}
698-
699-
retained = malloc(key_len);
700-
if (!retained) {
701-
errno = ENOMEM;
702-
return -1;
703-
}
704-
ret = derive_retained_key(hmac, hostnqn, configured, retained, key_len);
705-
if (ret > 0)
706-
ret = derive_tls_key(hmac, identity, retained, psk, key_len);
707-
free(retained);
708-
return ret;
709-
}
710708
#endif /* CONFIG_OPENSSL */
711709

712710
#ifdef CONFIG_OPENSSL_1
@@ -780,6 +778,15 @@ int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac,
780778
HMAC_CTX_free(hmac_ctx);
781779
return err;
782780
}
781+
782+
static int gen_tls_identity(const char *hostnqn, const char *subsysnqn,
783+
int hmac, char *identity,
784+
unsigned char *retained, size_t key_len)
785+
{
786+
sprintf(identity, "NVMe0R%02d %s %s",
787+
hmac, hostnqn, subsysnqn);
788+
return strlen(identity);
789+
}
783790
#endif /* !CONFIG_OPENSSL_1 */
784791

785792
#ifdef CONFIG_OPENSSL_3
@@ -874,9 +881,49 @@ int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac,
874881

875882
return err;
876883
}
884+
885+
static int gen_tls_identity(const char *hostnqn, const char *subsysnqn,
886+
int hmac, char *identity,
887+
unsigned char *retained, size_t key_len)
888+
{
889+
sprintf(identity, "NVMe0R%02d %s %s",
890+
version, hmac, hostnqn, subsysnqn);
891+
return strlen(identity);
892+
}
877893
#endif /* !CONFIG_OPENSSL_3 */
878894

879895
#ifdef CONFIG_KEYUTILS
896+
static int derive_nvme_keys(const char *hostnqn, const char *subsysnqn,
897+
char *identity,
898+
int hmac, unsigned char *configured,
899+
unsigned char *psk, int key_len)
900+
{
901+
unsigned char *retained;
902+
int ret = -1;
903+
904+
if (!hostnqn || !subsysnqn || !identity) {
905+
errno = EINVAL;
906+
return -1;
907+
}
908+
909+
retained = malloc(key_len);
910+
if (!retained) {
911+
errno = ENOMEM;
912+
return -1;
913+
}
914+
ret = derive_retained_key(hmac, hostnqn, configured, retained, key_len);
915+
if (ret < 0)
916+
goto out;
917+
ret = gen_tls_identity(hostnqn, subsysnqn, hmac,
918+
identity, retained, key_len);
919+
if (ret < 0)
920+
goto out;
921+
ret = derive_tls_key(hmac, identity, retained, psk, key_len);
922+
out:
923+
free(retained);
924+
return ret;
925+
}
926+
880927
long nvme_lookup_keyring(const char *keyring)
881928
{
882929
key_serial_t keyring_id;
@@ -922,28 +969,28 @@ long nvme_insert_tls_key(const char *keyring, const char *key_type,
922969
{
923970
key_serial_t keyring_id, key = 0;
924971
char *identity;
972+
size_t identity_len;
925973
unsigned char *psk;
926974
int ret = -1;
927975

928976
keyring_id = nvme_lookup_keyring(keyring);
929977
if (keyring_id == 0)
930978
return -1;
931979

932-
identity = malloc(strlen(hostnqn) + strlen(subsysnqn) + 12);
980+
identity_len = strlen(hostnqn) + strlen(subsysnqn) + 12;
981+
identity = malloc(identity_len);
933982
if (!identity) {
934983
errno = ENOMEM;
935984
return -1;
936985
}
937986

938-
sprintf(identity, "NVMe0R%02d %s %s", hmac, hostnqn, subsysnqn);
939-
940987
psk = malloc(key_len);
941988
if (!psk) {
942989
errno = ENOMEM;
943990
goto out_free_identity;
944991
}
945992
memset(psk, 0, key_len);
946-
ret = derive_nvme_keys(hostnqn, identity, hmac,
993+
ret = derive_nvme_keys(hostnqn, subsysnqn, identity, hmac,
947994
configured_key, psk, key_len);
948995
if (ret != key_len)
949996
goto out_free_psk;
@@ -1002,6 +1049,9 @@ long nvme_insert_tls_key(const char *keyring, const char *key_type,
10021049
const char *hostnqn, const char *subsysnqn, int hmac,
10031050
unsigned char *configured_key, int key_len)
10041051
{
1005-
return derive_nvme_keys(NULL, NULL, 0, NULL, NULL, 0);
1052+
nvme_msg(NULL, LOG_ERR, "key operations not supported; "
1053+
"recompile with keyutils support.\n");
1054+
errno = ENOTSUP;
1055+
return -1;
10061056
}
10071057
#endif

0 commit comments

Comments
 (0)