Skip to content

Commit b353738

Browse files
committed
fabrics: allow tls key to be a pin
For testing purpose it is very useful to have proper created secret keys based on a pin. Thus extend nvmf_context_set_crypto to transform the pin secret into a proper key. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 7b4f3f6 commit b353738

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

libnvme/src/nvme/fabrics.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ __public int nvmf_context_create(struct nvme_global_ctx *ctx,
204204
if (!fctx)
205205
return -ENOMEM;
206206

207+
fctx->ctx = ctx;
208+
207209
fctx->decide_retry = decide_retry;
208210
fctx->connected = connected;
209211
fctx->already_connected = already_connected;
@@ -216,6 +218,7 @@ __public int nvmf_context_create(struct nvme_global_ctx *ctx,
216218

217219
__public void nvmf_context_free(struct nvmf_context *fctx)
218220
{
221+
free(fctx->tls_key);
219222
free(fctx);
220223
}
221224

@@ -285,12 +288,37 @@ __public int nvmf_context_set_crypto(struct nvmf_context *fctx,
285288
const char *keyring, const char *tls_key,
286289
const char *tls_key_identity)
287290
{
291+
int err;
292+
288293
fctx->hostkey = hostkey;
289294
fctx->ctrlkey = ctrlkey;
290295
fctx->keyring = keyring;
291-
fctx->tls_key = tls_key;
292296
fctx->tls_key_identity = tls_key_identity;
293297

298+
if (!tls_key)
299+
return 0;
300+
301+
if (!strncmp(tls_key, "pin:", 4)) {
302+
_cleanup_free_ unsigned char *raw_secret = NULL;
303+
_cleanup_free_ char *encoded_key = NULL;
304+
int key_len = 32;
305+
306+
err = nvme_create_raw_secret(fctx->ctx, tls_key,
307+
key_len, &raw_secret);
308+
if (err)
309+
return err;
310+
311+
err = nvme_export_tls_key(fctx->ctx, raw_secret,
312+
key_len, &encoded_key);
313+
if (err)
314+
return err;
315+
316+
fctx->tls_key = encoded_key;
317+
encoded_key = NULL;
318+
return 0;
319+
}
320+
321+
fctx->tls_key = strdup(tls_key);
294322
return 0;
295323
}
296324

libnvme/src/nvme/private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ struct nvme_global_ctx {
307307
};
308308

309309
struct nvmf_context {
310+
struct nvme_global_ctx *ctx;
311+
310312
/* common callbacks */
311313
bool (*decide_retry)(struct nvmf_context *fctx, int err,
312314
void *user_data);
@@ -354,7 +356,7 @@ struct nvmf_context {
354356
const char *hostkey;
355357
const char *ctrlkey;
356358
const char *keyring;
357-
const char *tls_key;
359+
char *tls_key;
358360
const char *tls_key_identity;
359361

360362
void *user_data;

0 commit comments

Comments
 (0)