|
5 | 5 |
|
6 | 6 | #include <linux/module.h> |
7 | 7 | #include <linux/seq_file.h> |
8 | | -#include <linux/key.h> |
9 | 8 | #include <linux/key-type.h> |
10 | 9 | #include <keys/user-type.h> |
11 | 10 | #include <linux/nvme.h> |
@@ -124,6 +123,70 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring, |
124 | 123 | return key_ref_to_ptr(keyref); |
125 | 124 | } |
126 | 125 |
|
| 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 | + |
127 | 190 | /* |
128 | 191 | * NVMe PSK priority list |
129 | 192 | * |
|
0 commit comments