Skip to content

Commit 62eb893

Browse files
Hannes Reineckekeithbusch
authored andcommitted
nvme-keyring: add nvme_tls_psk_refresh()
Add a function to refresh a generated PSK in the specified keyring. Signed-off-by: Hannes Reinecke <[email protected]> Reviewed-by: Sagi Grimberg <[email protected]> Signed-off-by: Keith Busch <[email protected]>
1 parent 9d5c0ff commit 62eb893

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

drivers/nvme/common/keyring.c

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include <linux/module.h>
77
#include <linux/seq_file.h>
8-
#include <linux/key.h>
98
#include <linux/key-type.h>
109
#include <keys/user-type.h>
1110
#include <linux/nvme.h>
@@ -124,6 +123,70 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
124123
return key_ref_to_ptr(keyref);
125124
}
126125

126+
/**
127+
* nvme_tls_psk_refresh - Refresh TLS PSK
128+
* @keyring: Keyring holding the TLS PSK
129+
* @hostnqn: Host NQN to use
130+
* @subnqn: Subsystem NQN to use
131+
* @hmac_id: Hash function identifier
132+
* @data: TLS PSK key material
133+
* @data_len: Length of @data
134+
* @digest: TLS PSK digest
135+
*
136+
* Refresh a generated version 1 TLS PSK with the identity generated
137+
* from @hmac_id, @hostnqn, @subnqn, and @digest in the keyring given
138+
* by @keyring.
139+
*
140+
* Returns the updated key success or an error pointer otherwise.
141+
*/
142+
struct key *nvme_tls_psk_refresh(struct key *keyring,
143+
const char *hostnqn, const char *subnqn, u8 hmac_id,
144+
u8 *data, size_t data_len, const char *digest)
145+
{
146+
key_perm_t keyperm =
147+
KEY_POS_SEARCH | KEY_POS_VIEW | KEY_POS_READ |
148+
KEY_POS_WRITE | KEY_POS_LINK | KEY_POS_SETATTR |
149+
KEY_USR_SEARCH | KEY_USR_VIEW | KEY_USR_READ;
150+
char *identity;
151+
key_ref_t keyref;
152+
key_serial_t keyring_id;
153+
struct key *key;
154+
155+
if (!hostnqn || !subnqn || !data || !data_len)
156+
return ERR_PTR(-EINVAL);
157+
158+
identity = kasprintf(GFP_KERNEL, "NVMe1G%02d %s %s %s",
159+
hmac_id, hostnqn, subnqn, digest);
160+
if (!identity)
161+
return ERR_PTR(-ENOMEM);
162+
163+
if (!keyring)
164+
keyring = nvme_keyring;
165+
keyring_id = key_serial(keyring);
166+
pr_debug("keyring %x refresh tls psk '%s'\n",
167+
keyring_id, identity);
168+
keyref = key_create_or_update(make_key_ref(keyring, true),
169+
"psk", identity, data, data_len,
170+
keyperm, KEY_ALLOC_NOT_IN_QUOTA |
171+
KEY_ALLOC_BUILT_IN |
172+
KEY_ALLOC_BYPASS_RESTRICTION);
173+
if (IS_ERR(keyref)) {
174+
pr_debug("refresh tls psk '%s' failed, error %ld\n",
175+
identity, PTR_ERR(keyref));
176+
kfree(identity);
177+
return ERR_PTR(-ENOKEY);
178+
}
179+
kfree(identity);
180+
/*
181+
* Set the default timeout to 1 hour
182+
* as suggested in TP8018.
183+
*/
184+
key = key_ref_to_ptr(keyref);
185+
key_set_timeout(key, 3600);
186+
return key;
187+
}
188+
EXPORT_SYMBOL_GPL(nvme_tls_psk_refresh);
189+
127190
/*
128191
* NVMe PSK priority list
129192
*

drivers/nvme/host/tcp.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <linux/init.h>
99
#include <linux/slab.h>
1010
#include <linux/err.h>
11-
#include <linux/key.h>
1211
#include <linux/nvme-tcp.h>
1312
#include <linux/nvme-keyring.h>
1413
#include <net/sock.h>

drivers/nvme/target/tcp.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <linux/init.h>
99
#include <linux/slab.h>
1010
#include <linux/err.h>
11-
#include <linux/key.h>
1211
#include <linux/nvme-tcp.h>
1312
#include <linux/nvme-keyring.h>
1413
#include <net/sock.h>

include/linux/nvme-keyring.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66
#ifndef _NVME_KEYRING_H
77
#define _NVME_KEYRING_H
88

9+
#include <linux/key.h>
10+
911
#if IS_ENABLED(CONFIG_NVME_KEYRING)
1012

13+
struct key *nvme_tls_psk_refresh(struct key *keyring,
14+
const char *hostnqn, const char *subnqn, u8 hmac_id,
15+
u8 *data, size_t data_len, const char *digest);
1116
key_serial_t nvme_tls_psk_default(struct key *keyring,
1217
const char *hostnqn, const char *subnqn);
1318

1419
key_serial_t nvme_keyring_id(void);
1520
struct key *nvme_tls_key_lookup(key_serial_t key_id);
1621
#else
17-
22+
static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
23+
const char *hostnqn, char *subnqn, u8 hmac_id,
24+
u8 *data, size_t data_len, const char *digest)
25+
{
26+
return ERR_PTR(-ENOTSUPP);
27+
}
1828
static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
1929
const char *hostnqn, const char *subnqn)
2030
{

0 commit comments

Comments
 (0)