Skip to content

Commit 2cb1ed6

Browse files
hreineckeigaw
authored andcommitted
linux: add key helper functions
Add helper functions for key handling. Signed-off-by: Hannes Reinecke <[email protected]> [dwagner: - set errno on failure and updated documentation accordingly - fix return check of nvme_lookup_key in nvme_insert_tls_key] Signed-off-by: Daniel Wagner <[email protected]>
1 parent af874a2 commit 2cb1ed6

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

src/libnvme.map

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
LIBNVME_1_4 {
44
global:
55
nvme_lookup_keyring;
6+
nvme_describe_key_serial;
7+
nvme_lookup_key;
8+
nvme_set_keyring;
69
nvme_insert_tls_key;
710
};
811

src/nvme/linux.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,35 @@ long nvme_lookup_keyring(const char *keyring)
791791
return keyring_id;
792792
}
793793

794+
char *nvme_describe_key_serial(long key_id)
795+
{
796+
char *desc;
797+
798+
if (keyctl_describe_alloc(key_id, &desc) < 0)
799+
desc = NULL;
800+
return desc;
801+
}
802+
803+
long nvme_lookup_key(const char *type, const char *identity)
804+
{
805+
key_serial_t key;
806+
807+
key = keyctl_search(KEY_SPEC_SESSION_KEYRING, type, identity, 0);
808+
if (key < 0)
809+
return 0;
810+
return key;
811+
}
812+
813+
int nvme_set_keyring(long key_id)
814+
{
815+
long err;
816+
817+
err = keyctl_link(key_id, KEY_SPEC_SESSION_KEYRING);
818+
if (err < 0)
819+
return -1;
820+
return 0;
821+
}
822+
794823
long nvme_insert_tls_key(const char *keyring, const char *key_type,
795824
const char *hostnqn, const char *subsysnqn, int hmac,
796825
unsigned char *configured_key, int key_len)
@@ -801,7 +830,7 @@ long nvme_insert_tls_key(const char *keyring, const char *key_type,
801830
int ret = -1;
802831

803832
keyring_id = nvme_lookup_keyring(keyring);
804-
if (keyring_id < 0)
833+
if (keyring_id == 0)
805834
return -1;
806835

807836
identity = malloc(strlen(hostnqn) + strlen(subsysnqn) + 12);
@@ -849,6 +878,30 @@ long nvme_lookup_keyring(const char *keyring)
849878
return 0;
850879
}
851880

881+
char *nvme_describe_key_serial(long key_id)
882+
{
883+
nvme_msg(NULL, LOG_ERR, "key operations not supported; "\
884+
"recompile with keyutils support.\n");
885+
errno = ENOTSUP;
886+
return NULL;
887+
}
888+
889+
long nvme_lookup_key(const char *type, const char *identity)
890+
{
891+
nvme_msg(NULL, LOG_ERR, "key operations not supported; "\
892+
"recompile with keyutils support.\n");
893+
errno = ENOTSUP;
894+
return 0;
895+
}
896+
897+
int nvme_set_keyring(long key_id)
898+
{
899+
nvme_msg(NULL, LOG_ERR, "key operations not supported; "\
900+
"recompile with keyutils support.\n");
901+
errno = ENOTSUP;
902+
return -1;
903+
}
904+
852905
long nvme_insert_tls_key(const char *keyring, const char *key_type,
853906
const char *hostnqn, const char *subsysnqn, int hmac,
854907
unsigned char *configured_key, int key_len)

src/nvme/linux.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,43 @@ int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac,
205205
*/
206206
long nvme_lookup_keyring(const char *keyring);
207207

208+
/**
209+
* nvme_describe_key_serial() - Return key description
210+
* @key_id: Key serial number
211+
*
212+
* Fetches the description of the key or keyring identified
213+
* by the serial number @key_id.
214+
*
215+
* Return: The description of @key_id or NULL on failure.
216+
* The returned string needs to be freed by the caller.
217+
*/
218+
char *nvme_describe_key_serial(long key_id);
219+
220+
/**
221+
* nvme_lookup_key() - Lookup key serial number
222+
* @type: Key type
223+
* @identity: Key description
224+
*
225+
* Looks up the serial number of the key @identity
226+
* with type %type in the current session keyring.
227+
*
228+
* Return: The key serial number of the key
229+
* or 0 with errno set otherwise.
230+
*/
231+
long nvme_lookup_key(const char *type, const char *identity);
232+
233+
/**
234+
* nvme_set_keyring() - Link keyring for lookup
235+
* @keyring_id: Keyring id
236+
*
237+
* Links @keyring_id into the session keyring such that
238+
* its keys are available for further key lookups.
239+
*
240+
* Return: 0 on success, a negative number on error
241+
* with errno set.
242+
*/
243+
int nvme_set_keyring(long keyring_id);
244+
208245
/**
209246
* nvme_insert_tls_key() - Derive and insert TLS key
210247
* @keyring: Keyring to use

0 commit comments

Comments
 (0)