Debian Codegraph @

src/nvme/linux.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * This file is part of libnvme.
4 * Copyright (c) 2020 Western Digital Corporation or its affiliates.
5 *
6 * Authors: Keith Busch <[email protected]>
7 * Chaitanya Kulkarni <[email protected]>
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 #ifdef CONFIG_OPENSSL
21 #include <openssl/evp.h>
22 #include <openssl/hmac.h>
23 #include <openssl/kdf.h>
24 #include <openssl/core_names.h>
25 #include <openssl/params.h>
26 #endif
27
28 #ifdef CONFIG_KEYUTILS
29 #include <keyutils.h>
30
31 #define NVME_TLS_DEFAULT_KEYRING ".nvme"
32 #endif
33
34 #include <ccan/endian/endian.h>
35
36 #include "cleanup.h"
37 #include "linux.h"
38 #include "tree.h"
39 #include "log.h"
40 #include "private.h"
41 #include "base64.h"
42 #include "crc32.h"
43
44 static int __nvme_open(const char *name)
45 {
46 _cleanup_free_ char *path = NULL;
47 int ret;
48
49 ret = asprintf(&path, "%s/%s", "/dev", name);
50 if (ret < 0) {
51 errno = ENOMEM;
52 return -1;
53 }
54
55 return open(path, O_RDONLY);
56 }
57
58 int nvme_open(const char *name)
59 {
60 int ret, fd, id, ns;
61 struct stat stat;
62 bool c;
63
64 ret = sscanf(name, "nvme%dn%d", &id, &ns);
65 if (ret != 1 && ret != 2) {
66 errno = EINVAL;
67 return -1;
68 }
69 c = ret == 1;
70
71 fd = __nvme_open(name);
72 if (fd < 0)
73 return fd;
74
75 ret = fstat(fd, &stat);
76 if (ret < 0)
77 goto close_fd;
78
79 if (c) {
80 if (!S_ISCHR(stat.st_mode)) {
81 errno = EINVAL;
82 goto close_fd;
83 }
84 } else if (!S_ISBLK(stat.st_mode)) {
85 errno = EINVAL;
86 goto close_fd;
87 }
88
89 return fd;
90
91 close_fd:
92 close(fd);
93 return -1;
94 }
95
96 int nvme_fw_download_seq(int fd, __u32 size, __u32 xfer, __u32 offset,
97 void *buf)
98 {
99 int err = 0;
100 struct nvme_fw_download_args args = {
101 .args_size = sizeof(args),
102 .fd = fd,
103 .offset = offset,
104 .data_len = xfer,
105 .data = buf,
106 .timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
107 .result = NULL,
108 };
109
110 while (size > 0) {
111 args.data_len = MIN(xfer, size);
112 err = nvme_fw_download(&args);
113 if (err)
114 break;
115
116 args.data += xfer;
117 size -= xfer;
118 args.offset += xfer;
119 }
120
121 return err;
122 }
123
124 int nvme_set_etdas(int fd, bool *changed)
125 {
126 struct nvme_feat_host_behavior da4;
127 int err;
128
129 err = nvme_get_features_host_behavior(fd, 0, &da4, NULL);
130 if (err)
131 return err;
132
133 if (da4.etdas) {
134 *changed = false;
135 return 0;
136 }
137
138 da4.etdas = 1;
139
140 err = nvme_set_features_host_behavior(fd, 0, &da4);
141 if (err)
142 return err;
143
144 *changed = true;
145 return 0;
146 }
147
148 int nvme_clear_etdas(int fd, bool *changed)
149 {
150 struct nvme_feat_host_behavior da4;
151 int err;
152
153 err = nvme_get_features_host_behavior(fd, 0, &da4, NULL);
154 if (err)
155 return err;
156
157 if (!da4.etdas) {
158 *changed = false;
159 return 0;
160 }
161
162 da4.etdas = 0;
163 err = nvme_set_features_host_behavior(fd, 0, &da4);
164 if (err)
165 return err;
166
167 *changed = true;
168 return 0;
169 }
170
171 int nvme_get_uuid_list(int fd, struct nvme_id_uuid_list *uuid_list)
172 {
173 int err;
174 struct nvme_id_ctrl ctrl;
175
176 memset(&ctrl, 0, sizeof(struct nvme_id_ctrl));
177 err = nvme_identify_ctrl(fd, &ctrl);
178 if (err) {
179 fprintf(stderr, "ERROR: nvme_identify_ctrl() failed 0x%x\n", err);
180 return err;
181 }
182
183 if ((ctrl.ctratt & NVME_CTRL_CTRATT_UUID_LIST) == NVME_CTRL_CTRATT_UUID_LIST)
184 err = nvme_identify_uuid(fd, uuid_list);
185
186 return err;
187 }
188
189 int nvme_get_telemetry_max(int fd, enum nvme_telemetry_da *da, size_t *data_tx)
190 {
191 _cleanup_free_ struct nvme_id_ctrl *id_ctrl = NULL;
192 int err;
193
194 id_ctrl = __nvme_alloc(sizeof(*id_ctrl));
195 if (!id_ctrl) {
196 errno = ENOMEM;
197 return -1;
198 }
199 err = nvme_identify_ctrl(fd, id_ctrl);
200 if (err)
201 return err;
202
203 if (data_tx) {
204 *data_tx = id_ctrl->mdts;
205 if (id_ctrl->mdts) {
206 /* assuming CAP.MPSMIN is zero minimum Memory Page Size is at least
207 * 4096 bytes
208 */
209 *data_tx = (1 << id_ctrl->mdts) * 4096;
210 }
211 }
212 if (da) {
213 if (id_ctrl->lpa & 0x8)
214 *da = NVME_TELEMETRY_DA_3;
215 if (id_ctrl->lpa & 0x40)
216 *da = NVME_TELEMETRY_DA_4;
217
218 }
219 return err;
220 }
221
222 int nvme_get_telemetry_log(int fd, bool create, bool ctrl, bool rae, size_t max_data_tx,
223 enum nvme_telemetry_da da, struct nvme_telemetry_log **buf,
224 size_t *size)
225 {
226 static const __u32 xfer = NVME_LOG_TELEM_BLOCK_SIZE;
227
228 struct nvme_telemetry_log *telem;
229 enum nvme_cmd_get_log_lid lid;
230 _cleanup_free_ void *log = NULL;
231 void *tmp;
232 int err;
233 size_t dalb;
234 struct nvme_get_log_args args = {
235 .args_size = sizeof(args),
236 .fd = fd,
237 .nsid = NVME_NSID_NONE,
238 .lsp = NVME_LOG_LSP_NONE,
239 .lsi = NVME_LOG_LSI_NONE,
240 .uuidx = NVME_UUID_NONE,
241 .timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
242 .result = NULL,
243 .csi = NVME_CSI_NVM,
244 .rae = rae,
245 .ot = false,
246 };
247
248 *size = 0;
249
250 log = __nvme_alloc(xfer);
251 if (!log) {
252 errno = ENOMEM;
253 return -1;
254 }
255
256 if (ctrl) {
257 err = nvme_get_log_telemetry_ctrl(fd, true, 0, xfer, log);
258 lid = NVME_LOG_LID_TELEMETRY_CTRL;
259 } else {
260 lid = NVME_LOG_LID_TELEMETRY_HOST;
261 if (create)
262 err = nvme_get_log_create_telemetry_host_mcda(fd, da, log);
263 else
264 err = nvme_get_log_telemetry_host(fd, 0, xfer, log);
265 }
266
267 if (err)
268 return err;
269
270 telem = log;
271 if (ctrl && !telem->ctrlavail) {
272 *buf = log;
273 log = NULL;
274 *size = xfer;
275 return 0;
276 }
277
278 switch (da) {
279 case NVME_TELEMETRY_DA_1:
280 dalb = le16_to_cpu(telem->dalb1);
281 break;
282 case NVME_TELEMETRY_DA_2:
283 dalb = le16_to_cpu(telem->dalb2);
284 break;
285 case NVME_TELEMETRY_DA_3:
286 /* dalb3 >= dalb2 >= dalb1 */
287 dalb = le16_to_cpu(telem->dalb3);
288 break;
289 case NVME_TELEMETRY_DA_4:
290 dalb = le32_to_cpu(telem->dalb4);
291 break;
292 default:
293 errno = EINVAL;
294 return -1;
295 }
296
297 if (dalb == 0) {
298 errno = ENOENT;
299 return -1;
300 }
301
302 *size = (dalb + 1) * xfer;
303 tmp = __nvme_realloc(log, *size);
304 if (!tmp) {
305 errno = ENOMEM;
306 return -1;
307 }
308 log = tmp;
309
310 args.lid = lid;
311 args.log = log;
312 args.len = *size;
313 err = nvme_get_log_page(fd, max_data_tx, &args);
314 if (err)
315 return err;
316
317 *buf = log;
318 log = NULL;
319 return 0;
320 }
321
322
323 static int nvme_check_get_telemetry_log(int fd, bool create, bool ctrl, bool rae,
324 struct nvme_telemetry_log **log, enum nvme_telemetry_da da,
325 size_t *size)
326 {
327 enum nvme_telemetry_da max_da = 0;
328 int err = nvme_get_telemetry_max(fd, &max_da, NULL);
329
330 if (err)
331 return err;
332 if (da > max_da) {
333 errno = ENOENT;
334 return -1;
335 }
336 return nvme_get_telemetry_log(fd, create, ctrl, rae, 4096, da, log, size);
337 }
338
339
340 int nvme_get_ctrl_telemetry(int fd, bool rae, struct nvme_telemetry_log **log,
341 enum nvme_telemetry_da da, size_t *size)
342 {
343 return nvme_check_get_telemetry_log(fd, false, true, rae, log, da, size);
344 }
345
346 int nvme_get_host_telemetry(int fd, struct nvme_telemetry_log **log,
347 enum nvme_telemetry_da da, size_t *size)
348 {
349 return nvme_check_get_telemetry_log(fd, false, false, false, log, da, size);
350 }
351
352 int nvme_get_new_host_telemetry(int fd, struct nvme_telemetry_log **log,
353 enum nvme_telemetry_da da, size_t *size)
354 {
355 return nvme_check_get_telemetry_log(fd, true, false, false, log, da, size);
356 }
357
358 int nvme_get_lba_status_log(int fd, bool rae, struct nvme_lba_status_log **log)
359 {
360 _cleanup_free_ struct nvme_lba_status_log *buf = NULL;
361 __u32 size;
362 void *tmp;
363 int err;
364 struct nvme_get_log_args args = {
365 .args_size = sizeof(args),
366 .fd = fd,
367 .nsid = NVME_NSID_NONE,
368 .lsp = NVME_LOG_LSP_NONE,
369 .lsi = NVME_LOG_LSI_NONE,
370 .uuidx = NVME_UUID_NONE,
371 .timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
372 .result = NULL,
373 .csi = NVME_CSI_NVM,
374 .rae = rae,
375 .ot = false,
376 };
377
378 buf = malloc(sizeof(*buf));
379 if (!buf)
380 return -1;
381
382 err = nvme_get_log_lba_status(fd, true, 0, sizeof(*buf), buf);
383 if (err) {
384 *log = NULL;
385 return err;
386 }
387
388 size = le32_to_cpu(buf->lslplen);
389 if (!size) {
390 *log = buf;
391 buf = NULL;
392 return 0;
393 }
394
395 tmp = realloc(buf, size);
396 if (!tmp) {
397 *log = NULL;
398 return -1;
399 }
400 buf = tmp;
401
402 args.lid = NVME_LOG_LID_LBA_STATUS;
403 args.log = buf;
404 args.len = size;
405 err = nvme_get_log_page(fd, 4096, &args);
406 if (err) {
407 *log = NULL;
408 return err;
409 }
410
411 *log = buf;
412 buf = NULL;
413 return 0;
414 }
415
416 static int nvme_ns_attachment(int fd, __u32 nsid, __u16 num_ctrls,
417 __u16 *ctrlist, bool attach, __u32 timeout)
418 {
419 struct nvme_ctrl_list cntlist = { 0 };
420 struct nvme_ns_attach_args args = {
421 .args_size = sizeof(args),
422 .fd = fd,
423 .nsid = nsid,
424 .sel = NVME_NS_ATTACH_SEL_CTRL_DEATTACH,
425 .ctrlist = &cntlist,
426 .timeout = timeout,
427 };
428
429 if (attach)
430 args.sel = NVME_NS_ATTACH_SEL_CTRL_ATTACH;
431
432 nvme_init_ctrl_list(args.ctrlist, num_ctrls, ctrlist);
433 return nvme_ns_attach(&args);
434 }
435
436 int nvme_namespace_attach_ctrls(int fd, __u32 nsid, __u16 num_ctrls,
437 __u16 *ctrlist)
438 {
439 return nvme_ns_attachment(fd, nsid, num_ctrls, ctrlist, true,
440 NVME_DEFAULT_IOCTL_TIMEOUT);
441 }
442
443 int nvme_namespace_detach_ctrls(int fd, __u32 nsid, __u16 num_ctrls,
444 __u16 *ctrlist)
445 {
446 return nvme_ns_attachment(fd, nsid, num_ctrls, ctrlist, false,
447 NVME_DEFAULT_IOCTL_TIMEOUT);
448 }
449
450 size_t nvme_get_ana_log_len_from_id_ctrl(const struct nvme_id_ctrl *id_ctrl,
451 bool rgo)
452 {
453 __u32 nanagrpid = le32_to_cpu(id_ctrl->nanagrpid);
454 size_t size = sizeof(struct nvme_ana_log) +
455 nanagrpid * sizeof(struct nvme_ana_group_desc);
456
457 return rgo ? size : size + le32_to_cpu(id_ctrl->mnan) * sizeof(__le32);
458 }
459
460 int nvme_get_ana_log_len(int fd, size_t *analen)
461 {
462 _cleanup_free_ struct nvme_id_ctrl *ctrl = NULL;
463 int ret;
464
465 ctrl = __nvme_alloc(sizeof(*ctrl));
466 if (!ctrl) {
467 errno = ENOMEM;
468 return -1;
469 }
470 ret = nvme_identify_ctrl(fd, ctrl);
471 if (ret)
472 return ret;
473
474 *analen = nvme_get_ana_log_len_from_id_ctrl(ctrl, false);
475 return 0;
476 }
477
478 int nvme_get_logical_block_size(int fd, __u32 nsid, int *blksize)
479 {
480 _cleanup_free_ struct nvme_id_ns *ns = NULL;
481 __u8 flbas;
482 int ret;
483
484 ns = __nvme_alloc(sizeof(*ns));
485 if (!ns) {
486 errno = ENOMEM;
487 return -1;
488 }
489 ret = nvme_identify_ns(fd, nsid, ns);
490 if (ret)
491 return ret;
492
493 nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &flbas);
494 *blksize = 1 << ns->lbaf[flbas].ds;
495
496 return 0;
497 }
498
499 static int __nvme_set_attr(const char *path, const char *value)
500 {
501 _cleanup_fd_ int fd = -1;
502
503 fd = open(path, O_WRONLY);
504 if (fd < 0) {
505 #if 0
506 nvme_msg(LOG_DEBUG, "Failed to open %s: %s\n", path,
507 strerror(errno));
508 #endif
509 return -1;
510 }
511 return write(fd, value, strlen(value));
512 }
513
514 int nvme_set_attr(const char *dir, const char *attr, const char *value)
515 {
516 _cleanup_free_ char *path = NULL;
517 int ret;
518
519 ret = asprintf(&path, "%s/%s", dir, attr);
520 if (ret < 0)
521 return -1;
522
523 return __nvme_set_attr(path, value);
524 }
525
526 static char *__nvme_get_attr(const char *path)
527 {
528 char value[4096] = { 0 };
529 int ret, fd;
530 int saved_errno;
531
532 fd = open(path, O_RDONLY);
533 if (fd < 0)
534 return NULL;
535
536 ret = read(fd, value, sizeof(value) - 1);
537 saved_errno = errno;
538 close(fd);
539 if (ret < 0) {
540 errno = saved_errno;
541 return NULL;
542 }
543 errno = 0;
544 if (!strlen(value))
545 return NULL;
546
547 if (value[strlen(value) - 1] == '\n')
548 value[strlen(value) - 1] = '\0';
549 while (strlen(value) > 0 && value[strlen(value) - 1] == ' ')
550 value[strlen(value) - 1] = '\0';
551
552 return strlen(value) ? strdup(value) : NULL;
553 }
554
555 char *nvme_get_attr(const char *dir, const char *attr)
556 {
557 _cleanup_free_ char *path = NULL;
558 int ret;
559
560 ret = asprintf(&path, "%s/%s", dir, attr);
561 if (ret < 0) {
562 errno = ENOMEM;
563 return NULL;
564 }
565
566 return __nvme_get_attr(path);
567 }
568
569 char *nvme_get_subsys_attr(nvme_subsystem_t s, const char *attr)
570 {
571 return nvme_get_attr(nvme_subsystem_get_sysfs_dir(s), attr);
572 }
573
574 char *nvme_get_ctrl_attr(nvme_ctrl_t c, const char *attr)
575 {
576 return nvme_get_attr(nvme_ctrl_get_sysfs_dir(c), attr);
577 }
578
579 char *nvme_get_ns_attr(nvme_ns_t n, const char *attr)
580 {
581 return nvme_get_attr(nvme_ns_get_sysfs_dir(n), attr);
582 }
583
584 char *nvme_get_path_attr(nvme_path_t p, const char *attr)
585 {
586 return nvme_get_attr(nvme_path_get_sysfs_dir(p), attr);
587 }
588
589 #ifndef CONFIG_OPENSSL
590 static unsigned char default_hmac(size_t key_len)
591 {
592 return NVME_HMAC_ALG_NONE;
593 }
594
595 int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac,
596 unsigned int key_len, unsigned char *secret,
597 unsigned char *key)
598 {
599 if (hmac != NVME_HMAC_ALG_NONE) {
600 nvme_msg(NULL, LOG_ERR, "HMAC transformation not supported; " \
601 "recompile with OpenSSL support.\n");
602 errno = -EINVAL;
603 return -1;
604 }
605
606 memcpy(key, secret, key_len);
607 return 0;
608 }
609
610 static int derive_retained_key(int hmac, const char *hostnqn,
611 unsigned char *generated,
612 unsigned char *retained,
613 size_t key_len)
614 {
615 nvme_msg(NULL, LOG_ERR, "NVMe TLS is not supported; "
616 "recompile with OpenSSL support.\n");
617 errno = ENOTSUP;
618 return -1;
619 }
620
621 static int derive_retained_key_compat(int hmac, const char *hostnqn,
622 unsigned char *generated,
623 unsigned char *retained,
624 size_t key_len)
625 {
626 nvme_msg(NULL, LOG_ERR, "NVMe TLS is not supported; "
627 "recompile with OpenSSL support.\n");
628 errno = ENOTSUP;
629 return -1;
630 }
631
632 static int derive_psk_digest(const char *hostnqn, const char *subsysnqn,
633 int version, int cipher,
634 unsigned char *retained, size_t key_len,
635 char *digest, size_t digest_len)
636 {
637 nvme_msg(NULL, LOG_ERR, "NVMe TLS 2.0 is not supported; "
638 "recompile with OpenSSL support.\n");
639 errno = ENOTSUP;
640 return -1;
641 }
642
643 static int derive_tls_key(int version, int cipher, const char *context,
644 unsigned char *retained,
645 unsigned char *psk, size_t key_len)
646 {
647 nvme_msg(NULL, LOG_ERR, "NVMe TLS is not supported; "
648 "recompile with OpenSSL support.\n");
649 errno = ENOTSUP;
650 return -1;
651 }
652
653 static int derive_tls_key_compat(int version, int cipher, const char *context,
654 unsigned char *retained,
655 unsigned char *psk, size_t key_len)
656 {
657 nvme_msg(NULL, LOG_ERR, "NVMe TLS is not supported; "
658 "recompile with OpenSSL support.\n");
659 errno = ENOTSUP;
660 return -1;
661 }
662 #else /* CONFIG_OPENSSL */
663 static unsigned char default_hmac(size_t key_len)
664 {
665 unsigned char hmac = NVME_HMAC_ALG_NONE;
666
667 switch (key_len) {
668 case 32:
669 hmac = NVME_HMAC_ALG_SHA2_256;
670 break;
671 case 48:
672 hmac = NVME_HMAC_ALG_SHA2_384;
673 break;
674 case 64:
675 hmac = NVME_HMAC_ALG_SHA2_512;
676 break;
677 default:
678 break;
679 }
680 return hmac;
681 }
682
683 static const EVP_MD *select_hmac(int hmac, size_t *hmac_len)
684 {
685 const EVP_MD *md = NULL;
686
687 switch (hmac) {
688 case NVME_HMAC_ALG_SHA2_256:
689 md = EVP_sha256();
690 *hmac_len = 32;
691 break;
692 case NVME_HMAC_ALG_SHA2_384:
693 md = EVP_sha384();
694 *hmac_len = 48;
695 break;
696 default:
697 *hmac_len = 0;
698 break;
699 }
700 return md;
701 }
702
703 static DEFINE_CLEANUP_FUNC(
704 cleanup_evp_pkey_ctx, EVP_PKEY_CTX *, EVP_PKEY_CTX_free)
705 #define _cleanup_evp_pkey_ctx_ __cleanup__(cleanup_evp_pkey_ctx)
706
707 /* NVMe is using the TLS 1.3 HkdfLabel structure */
708 #define HKDF_INFO_MAX_LEN 514
709 #define HKDF_INFO_LABEL_MAX 256
710 #define HKDF_INFO_CONTEXT_MAX 256
711
712 /*
713 * derive_retained_key()
714 *
715 * Derive a retained key according to NVMe TCP Transport specification:
716 *
717 * The retained PSK is derived from the configured PSK. The configured PSK
718 * shall be destroyed as soon as the retained PSK is generated and stored.
719 * Each NVMe/TCP entity shall support:
720 * 1) transforming the configured PSK into a retained PSK before it is stored
721 * by the NVMe/TCP entity for repeated use with another NVMe/TCP entity; and
722 * 2) using the configured PSK as a retained PSK.
723 *
724 * The method to derive a retained PSK from a configured PSK shall be using
725 * the HKDF-Extract and HKDF-Expand-Label operations (refer to RFC 5869 and
726 * RFC 8446):
727 * 1. PRK = HKDF-Extract(0, Configured PSK); and
728 * 2. Retained PSK = HKDF-Expand-Label(PRK, “HostNQN”, NQNh,
729 * Length(Configured PSK)),
730 * where NQNh is the NQN of the host.
731 *
732 * 'hmac' indicates the hash function to be used to transform the configured
733 * PSK in a retained PSK, encoded as follows:
734 *
735 * - 0 indicates no transform (i.e., the configured PSK is used as a
736 * retained PSK)
737 * - 1 indicates SHA-256
738 * - 2 indicates SHA-384
739 */
740 static int derive_retained_key(int hmac, const char *hostnqn,
741 unsigned char *configured,
742 unsigned char *retained,
743 size_t key_len)
744 {
745 _cleanup_evp_pkey_ctx_ EVP_PKEY_CTX *ctx = NULL;
746 _cleanup_free_ uint8_t *hkdf_info = NULL;
747 char *hkdf_label;
748 const EVP_MD *md;
749 size_t hmac_len;
750 char *pos;
751 int ret;
752
753 /* +1 byte so that the snprintf terminating null can not overflow */
754 hkdf_info = malloc(HKDF_INFO_MAX_LEN + 1);
755 if (!hkdf_info) {
756 errno = ENOMEM;
757 return -1;
758 }
759
760 if (hmac == NVME_HMAC_ALG_NONE) {
761 memcpy(retained, configured, key_len);
762 return key_len;
763 }
764
765 md = select_hmac(hmac, &hmac_len);
766 if (!md || !hmac_len) {
767 errno = EINVAL;
768 return -1;
769 }
770
771 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
772 if (!ctx) {
773 errno = ENOMEM;
774 return -1;
775 }
776
777 if (EVP_PKEY_derive_init(ctx) <= 0) {
778 errno = ENOMEM;
779 return -1;
780 }
781 if (EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0) {
782 errno = ENOKEY;
783 return -1;
784 }
785 if (EVP_PKEY_CTX_set1_hkdf_key(ctx, configured, key_len) <= 0) {
786 errno = ENOKEY;
787 return -1;
788 }
789
790 if (key_len > USHRT_MAX) {
791 errno = EINVAL;
792 return -1;
793 }
794 pos = (char *)hkdf_info;
795 *(uint16_t *)pos = htons(key_len & 0xFFFF);
796 pos += sizeof(uint16_t);
797
798 hkdf_label = "tls13 HostNQN";
799 ret = snprintf(pos, HKDF_INFO_LABEL_MAX + 1, "%c%s",
800 (int)strlen(hkdf_label), hkdf_label);
801 if (ret <= 0 || ret > HKDF_INFO_LABEL_MAX) {
802 errno = ENOKEY;
803 return -1;
804 }
805 pos += ret;
806
807 ret = snprintf(pos, HKDF_INFO_CONTEXT_MAX + 1, "%c%s",
808 (int)strlen(hostnqn), hostnqn);
809 if (ret <= 0 || ret > HKDF_INFO_CONTEXT_MAX) {
810 errno = ENOKEY;
811 return -1;
812 }
813 pos += ret;
814
815 if (EVP_PKEY_CTX_add1_hkdf_info(ctx, hkdf_info,
816 (pos - (char *)hkdf_info)) <= 0) {
817 errno = ENOKEY;
818 return -1;
819 }
820
821 if (EVP_PKEY_derive(ctx, retained, &key_len) <= 0) {
822 errno = ENOKEY;
823 return -1;
824 }
825
826 return key_len;
827 }
828
829 static int derive_retained_key_compat(int hmac, const char *hostnqn,
830 unsigned char *configured,
831 unsigned char *retained,
832 size_t key_len)
833 {
834 _cleanup_evp_pkey_ctx_ EVP_PKEY_CTX *ctx = NULL;
835 _cleanup_free_ uint8_t *hkdf_info = NULL;
836 const EVP_MD *md;
837 size_t hmac_len;
838 char *pos;
839 int ret;
840
841 if (hmac == NVME_HMAC_ALG_NONE) {
842 memcpy(retained, configured, key_len);
843 return key_len;
844 }
845
846 md = select_hmac(hmac, &hmac_len);
847 if (!md || !hmac_len) {
848 errno = EINVAL;
849 return -1;
850 }
851
852 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
853 if (!ctx) {
854 errno = ENOMEM;
855 return -1;
856 }
857
858 if (EVP_PKEY_derive_init(ctx) <= 0) {
859 errno = ENOMEM;
860 return -1;
861 }
862 if (EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0) {
863 errno = ENOKEY;
864 return -1;
865 }
866 if (EVP_PKEY_CTX_set1_hkdf_key(ctx, configured, key_len) <= 0) {
867 errno = ENOKEY;
868 return -1;
869 }
870
871 /* +1 byte so that the snprintf terminating null can not overflow */
872 hkdf_info = malloc(HKDF_INFO_MAX_LEN + 1);
873 if (!hkdf_info) {
874 errno = ENOMEM;
875 return -1;
876 }
877
878 pos = (char *)hkdf_info;
879 *(uint16_t *)pos = cpu_to_le16(key_len);
880 pos += sizeof(uint16_t);
881
882 ret = snprintf(pos, HKDF_INFO_LABEL_MAX + 1,
883 "tls13 HostNQN%s", hostnqn);
884 if (ret <= 0 || ret > HKDF_INFO_LABEL_MAX) {
885 errno = ENOKEY;
886 return -1;
887 }
888 pos += ret;
889
890 if (EVP_PKEY_CTX_add1_hkdf_info(ctx, hkdf_info,
891 (pos - (char *)hkdf_info)) <= 0) {
892 errno = ENOKEY;
893 return -1;
894 }
895
896 if (EVP_PKEY_derive(ctx, retained, &key_len) <= 0) {
897 errno = ENOKEY;
898 return -1;
899 }
900
901 return key_len;
902 }
903
904 /*
905 * derive_tls_key()
906 *
907 * Derive a TLS PSK from a retained PSK.
908 *
909 * The TLS PSK shall be derived as follows from an input PSK (i.e., either
910 * a retained PSK or a generated PSK) and a PSK identity using the HKDF-Extract
911 * and HKDF-Expand-Label operations (refer to RFC 5869 and RFC 8446) where the
912 * hash function is the one specified by the hash specifier of the PSK identity:
913 * 1. PRK = HKDF-Extract(0, Input PSK); and
914 * 2. TLS PSK = HKDF-Expand-Label(PRK, “nvme-tls-psk”, PskIdentity, L),
915 * where PskIdentity is the PSK identity and L is the output size in bytes of
916 * the hash function (i.e., 32 for SHA-256 and 48 for SHA-384).
917 *
918 * Note that this is _not_ the hash value as specified by the configured key,
919 * but rather the hash function of the cipher suite associated with the
920 * PSK:
921 * - 1 indicates SHA-245 (for the TLS_AES_128_GCM_SHA256 cipher suite)
922 * - 2 indicates SHA-384 (for the TLS_AES_256_GCM_SHA384 cipher suite)
923 *
924 * and the value '0' is invalid here.
925 */
926
927 static int derive_tls_key(int version, unsigned char cipher,
928 const char *context, unsigned char *retained,
929 unsigned char *psk, size_t key_len)
930 {
931 _cleanup_evp_pkey_ctx_ EVP_PKEY_CTX *ctx = NULL;
932 _cleanup_free_ uint8_t *hkdf_info = NULL;
933 char *hkdf_label;
934 const EVP_MD *md;
935 size_t hmac_len;
936 char *pos;
937 int ret;
938
939 /* +1 byte so that the snprintf terminating null can not overflow */
940 hkdf_info = malloc(HKDF_INFO_MAX_LEN + 1);
941 if (!hkdf_info) {
942 errno = ENOMEM;
943 return -1;
944 }
945
946 md = select_hmac(cipher, &hmac_len);
947 if (!md || !hmac_len) {
948 errno = EINVAL;
949 return -1;
950 }
951
952 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
953 if (!ctx) {
954 errno = ENOMEM;
955 return -1;
956 }
957
958 if (EVP_PKEY_derive_init(ctx) <= 0) {
959 errno = ENOMEM;
960 return -1;
961 }
962 if (EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0) {
963 errno = ENOKEY;
964 return -1;
965 }
966 if (EVP_PKEY_CTX_set1_hkdf_key(ctx, retained, key_len) <= 0) {
967 errno = ENOKEY;
968 return -1;
969 }
970
971 if (key_len > USHRT_MAX) {
972 errno = EINVAL;
973 return -1;
974 }
975 pos = (char *)hkdf_info;
976 *(uint16_t *)pos = htons(key_len & 0xFFFF);
977 pos += sizeof(uint16_t);
978
979 hkdf_label = "tls13 nvme-tls-psk";
980 ret = snprintf(pos, HKDF_INFO_LABEL_MAX + 1, "%c%s",
981 (int)strlen(hkdf_label), hkdf_label);
982 if (ret <= 0 || ret > HKDF_INFO_LABEL_MAX) {
983 errno = ENOKEY;
984 return -1;
985 }
986 pos += ret;
987
988 switch (version) {
989 case 0:
990 ret = snprintf(pos, HKDF_INFO_CONTEXT_MAX + 1, "%c%s",
991 (int)strlen(context), context);
992 if (ret <= 0 || ret > HKDF_INFO_CONTEXT_MAX) {
993 errno = ENOKEY;
994 return -1;
995 }
996 pos += ret;
997 break;
998 case 1:
999 ret = snprintf(pos, HKDF_INFO_CONTEXT_MAX + 1, "%c%02d %s",
1000 (int)strlen(context) + 3, cipher, context);
1001 if (ret <= 0 || ret > HKDF_INFO_CONTEXT_MAX) {
1002 errno = ENOKEY;
1003 return -1;
1004 }
1005 pos += ret;
1006 break;
1007 default:
1008 errno = ENOKEY;
1009 return -1;
1010 }
1011
1012 if (EVP_PKEY_CTX_add1_hkdf_info(ctx, hkdf_info,
1013 (pos - (char *)hkdf_info)) <= 0) {
1014 errno = ENOKEY;
1015 return -1;
1016 }
1017 if (EVP_PKEY_derive(ctx, psk, &key_len) <= 0) {
1018 errno = ENOKEY;
1019 return -1;
1020 }
1021
1022 return key_len;
1023 }
1024
1025 static int derive_tls_key_compat(int version, unsigned char cipher,
1026 const char *context, unsigned char *retained,
1027 unsigned char *psk, size_t key_len)
1028 {
1029 _cleanup_evp_pkey_ctx_ EVP_PKEY_CTX *ctx = NULL;
1030 _cleanup_free_ uint8_t *hkdf_info = NULL;
1031 const EVP_MD *md;
1032 size_t hmac_len;
1033 char *pos;
1034 int ret;
1035
1036 md = select_hmac(cipher, &hmac_len);
1037 if (!md || !hmac_len) {
1038 errno = EINVAL;
1039 return -1;
1040 }
1041
1042 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
1043 if (!ctx) {
1044 errno = ENOMEM;
1045 return -1;
1046 }
1047
1048 if (EVP_PKEY_derive_init(ctx) <= 0) {
1049 errno = ENOMEM;
1050 return -1;
1051 }
1052 if (EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0) {
1053 errno = ENOKEY;
1054 return -1;
1055 }
1056 if (EVP_PKEY_CTX_set1_hkdf_key(ctx, retained, key_len) <= 0) {
1057 errno = ENOKEY;
1058 return -1;
1059 }
1060
1061 /* +1 byte so that the snprintf terminating null can not overflow */
1062 hkdf_info = malloc(HKDF_INFO_MAX_LEN + 1);
1063 if (!hkdf_info) {
1064 errno = ENOMEM;
1065 return -1;
1066 }
1067
1068 pos = (char *)hkdf_info;
1069 *(uint16_t *)pos = cpu_to_le16(key_len);
1070 pos += sizeof(uint16_t);
1071
1072 ret = snprintf(pos, HKDF_INFO_LABEL_MAX + 1, "tls13 nvme-tls-psk");
1073 if (ret <= 0 || ret > HKDF_INFO_LABEL_MAX) {
1074 errno = ENOKEY;
1075 return -1;
1076 }
1077 pos += ret;
1078
1079 switch (version) {
1080 case 0:
1081 ret = snprintf(pos, HKDF_INFO_CONTEXT_MAX + 1, "%s", context);
1082 if (ret <= 0 || ret > HKDF_INFO_CONTEXT_MAX) {
1083 errno = ENOKEY;
1084 return -1;
1085 }
1086 pos += ret;
1087 break;
1088 case 1:
1089 ret = snprintf(pos, HKDF_INFO_CONTEXT_MAX + 1, "%02d %s",
1090 cipher, context);
1091 if (ret <= 0 || ret > HKDF_INFO_CONTEXT_MAX) {
1092 errno = ENOKEY;
1093 return -1;
1094 }
1095 pos += ret;
1096 break;
1097 default:
1098 errno = ENOKEY;
1099 return -1;
1100 }
1101
1102 if (EVP_PKEY_CTX_add1_hkdf_info(ctx, hkdf_info,
1103 (pos - (char *)hkdf_info)) <= 0) {
1104 errno = ENOKEY;
1105 return -1;
1106 }
1107
1108 if (EVP_PKEY_derive(ctx, psk, &key_len) <= 0) {
1109 errno = ENOKEY;
1110 return -1;
1111 }
1112
1113 return key_len;
1114 }
1115
1116 static DEFINE_CLEANUP_FUNC(
1117 cleanup_ossl_lib_ctx, OSSL_LIB_CTX *, OSSL_LIB_CTX_free)
1118 #define _cleanup_ossl_lib_ctx_ __cleanup__(cleanup_ossl_lib_ctx)
1119 static DEFINE_CLEANUP_FUNC(cleanup_evp_mac_ctx, EVP_MAC_CTX *, EVP_MAC_CTX_free)
1120 #define _cleanup_evp_mac_ctx_ __cleanup__(cleanup_evp_mac_ctx)
1121 static DEFINE_CLEANUP_FUNC(cleanup_evp_mac, EVP_MAC *, EVP_MAC_free)
1122 #define _cleanup_evp_mac_ __cleanup__(cleanup_evp_mac)
1123
1124 int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac,
1125 unsigned int key_len, unsigned char *secret,
1126 unsigned char *key)
1127 {
1128 const char hmac_seed[] = "NVMe-over-Fabrics";
1129 _cleanup_ossl_lib_ctx_ OSSL_LIB_CTX *lib_ctx = NULL;
1130 _cleanup_evp_mac_ctx_ EVP_MAC_CTX *mac_ctx = NULL;
1131 _cleanup_evp_mac_ EVP_MAC *mac = NULL;
1132 OSSL_PARAM params[2], *p = params;
1133 char *progq = NULL;
1134 char *digest;
1135 size_t len;
1136
1137 lib_ctx = OSSL_LIB_CTX_new();
1138 if (!lib_ctx) {
1139 errno = ENOMEM;
1140 return -1;
1141 }
1142
1143 mac = EVP_MAC_fetch(lib_ctx, OSSL_MAC_NAME_HMAC, progq);
1144 if (!mac) {
1145 errno = ENOMEM;
1146 return -1;
1147 }
1148
1149 mac_ctx = EVP_MAC_CTX_new(mac);
1150 if (!mac_ctx) {
1151 errno = ENOMEM;
1152 return -1;
1153 }
1154
1155 switch (hmac) {
1156 case NVME_HMAC_ALG_NONE:
1157 memcpy(key, secret, key_len);
1158 return 0;
1159 case NVME_HMAC_ALG_SHA2_256:
1160 digest = OSSL_DIGEST_NAME_SHA2_256;
1161 break;
1162 case NVME_HMAC_ALG_SHA2_384:
1163 digest = OSSL_DIGEST_NAME_SHA2_384;
1164 break;
1165 case NVME_HMAC_ALG_SHA2_512:
1166 digest = OSSL_DIGEST_NAME_SHA2_512;
1167 break;
1168 default:
1169 errno = EINVAL;
1170 return -1;
1171 }
1172 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1173 digest,
1174 0);
1175 *p = OSSL_PARAM_construct_end();
1176
1177 if (!EVP_MAC_init(mac_ctx, secret, key_len, params)) {
1178 errno = ENOKEY;
1179 return -1;
1180 }
1181
1182 if (!EVP_MAC_update(mac_ctx, (unsigned char *)hostnqn,
1183 strlen(hostnqn))) {
1184 errno = ENOKEY;
1185 return -1;
1186 }
1187
1188 if (!EVP_MAC_update(mac_ctx, (unsigned char *)hmac_seed,
1189 strlen(hmac_seed))) {
1190 errno = ENOKEY;
1191 return -1;
1192 }
1193
1194 if (!EVP_MAC_final(mac_ctx, key, &len, key_len)) {
1195 errno = ENOKEY;
1196 return -1;
1197 }
1198
1199 if (len != key_len) {
1200 errno = EMSGSIZE;
1201 return -1;
1202 }
1203
1204 return 0;
1205 }
1206
1207 static int derive_psk_digest(const char *hostnqn, const char *subsysnqn,
1208 int version, int cipher,
1209 unsigned char *retained, size_t key_len,
1210 char *digest, size_t digest_len)
1211 {
1212 static const char hmac_seed[] = "NVMe-over-Fabrics";
1213 _cleanup_ossl_lib_ctx_ OSSL_LIB_CTX *lib_ctx = NULL;
1214 _cleanup_evp_mac_ctx_ EVP_MAC_CTX *mac_ctx = NULL;
1215 _cleanup_free_ unsigned char *psk_ctx = NULL;
1216 _cleanup_evp_mac_ EVP_MAC *mac = NULL;
1217 OSSL_PARAM params[2], *p = params;
1218 size_t hmac_len;
1219 char *progq = NULL;
1220 char *dig = NULL;
1221 size_t len;
1222
1223 lib_ctx = OSSL_LIB_CTX_new();
1224 if (!lib_ctx) {
1225 errno = ENOMEM;
1226 return -1;
1227 }
1228 mac = EVP_MAC_fetch(lib_ctx, OSSL_MAC_NAME_HMAC, progq);
1229 if (!mac) {
1230 errno = ENOMEM;
1231 return -1;
1232 }
1233
1234 mac_ctx = EVP_MAC_CTX_new(mac);
1235 if (!mac_ctx) {
1236 errno = ENOMEM;
1237 return -1;
1238 }
1239 switch (cipher) {
1240 case NVME_HMAC_ALG_SHA2_256:
1241 dig = OSSL_DIGEST_NAME_SHA2_256;
1242 break;
1243 case NVME_HMAC_ALG_SHA2_384:
1244 dig = OSSL_DIGEST_NAME_SHA2_384;
1245 break;
1246 default:
1247 errno = EINVAL;
1248 break;
1249 }
1250 if (!dig)
1251 return -1;
1252 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1253 dig, 0);
1254 *p = OSSL_PARAM_construct_end();
1255
1256 psk_ctx = malloc(key_len);
1257 if (!psk_ctx) {
1258 errno = ENOMEM;
1259 return -1;
1260 }
1261
1262 if (!EVP_MAC_init(mac_ctx, retained, key_len, params)) {
1263 errno = ENOKEY;
1264 return -1;
1265 }
1266 if (!EVP_MAC_update(mac_ctx, (unsigned char *)hostnqn,
1267 strlen(hostnqn))) {
1268 errno = ENOKEY;
1269 return -1;
1270 }
1271 if (!EVP_MAC_update(mac_ctx, (unsigned char *)" ", 1)) {
1272 errno = ENOKEY;
1273 return -1;
1274 }
1275 if (!EVP_MAC_update(mac_ctx, (unsigned char *)subsysnqn,
1276 strlen(subsysnqn))) {
1277 errno = ENOKEY;
1278 return -1;
1279 }
1280 if (!EVP_MAC_update(mac_ctx, (unsigned char *)" ", 1)) {
1281 errno = ENOKEY;
1282 return -1;
1283 }
1284 if (!EVP_MAC_update(mac_ctx, (unsigned char *)hmac_seed,
1285 strlen(hmac_seed))) {
1286 errno = ENOKEY;
1287 return -1;
1288 }
1289 if (!EVP_MAC_final(mac_ctx, psk_ctx, &hmac_len, key_len)) {
1290 errno = ENOKEY;
1291 return -1;
1292 }
1293 if (hmac_len > key_len) {
1294 errno = EMSGSIZE;
1295 return -1;
1296 }
1297 if (hmac_len * 2 > digest_len) {
1298 errno = EINVAL;
1299 return -1;
1300 }
1301 memset(digest, 0, digest_len);
1302 len = base64_encode(psk_ctx, hmac_len, digest);
1303 if (len < 0) {
1304 errno = ENOKEY;
1305 return len;
1306 }
1307 return strlen(digest);
1308 }
1309 #endif /* !CONFIG_OPENSSL */
1310
1311 static int gen_tls_identity(const char *hostnqn, const char *subsysnqn,
1312 int version, int cipher, char *digest,
1313 char *identity)
1314 {
1315 if (version == 0) {
1316 sprintf(identity, "NVMe%01dR%02d %s %s",
1317 version, cipher, hostnqn, subsysnqn);
1318 return strlen(identity);
1319 }
1320 if (version > 1 || !digest) {
1321 errno = EINVAL;
1322 return -1;
1323 }
1324
1325 sprintf(identity, "NVMe%01dR%02d %s %s %s",
1326 version, cipher, hostnqn, subsysnqn, digest);
1327 return strlen(identity);
1328 }
1329
1330 static int derive_nvme_keys(const char *hostnqn, const char *subsysnqn,
1331 char *identity, int version,
1332 int hmac, unsigned char *configured,
1333 unsigned char *psk, int key_len, bool compat)
1334 {
1335 _cleanup_free_ unsigned char *retained = NULL;
1336 _cleanup_free_ char *digest = NULL;
1337 char *context = identity;
1338 unsigned char cipher;
1339 int ret = -1;
1340
1341 if (!hostnqn || !subsysnqn || !identity || !psk) {
1342 errno = EINVAL;
1343 return -1;
1344 }
1345
1346 retained = malloc(key_len);
1347 if (!retained) {
1348 errno = ENOMEM;
1349 return -1;
1350 }
1351 if (compat)
1352 ret = derive_retained_key_compat(hmac, hostnqn, configured,
1353 retained, key_len);
1354 else
1355 ret = derive_retained_key(hmac, hostnqn, configured,
1356 retained, key_len);
1357 if (ret < 0)
1358 return ret;
1359
1360 if (hmac == NVME_HMAC_ALG_NONE)
1361 cipher = default_hmac(key_len);
1362 else
1363 cipher = hmac;
1364
1365 if (version == 1) {
1366 size_t digest_len = 2 * key_len;
1367
1368 digest = malloc(digest_len);
1369 if (!digest) {
1370 errno = ENOMEM;
1371 return -1;
1372 }
1373 ret = derive_psk_digest(hostnqn, subsysnqn, version, cipher,
1374 retained, key_len,
1375 digest, digest_len);
1376 if (ret < 0)
1377 return ret;
1378 context = digest;
1379 }
1380 ret = gen_tls_identity(hostnqn, subsysnqn, version, cipher,
1381 digest, identity);
1382 if (ret < 0)
1383 return ret;
1384 if (compat)
1385 return derive_tls_key_compat(version, cipher, context, retained,
1386 psk, key_len);
1387 return derive_tls_key(version, cipher, context, retained,
1388 psk, key_len);
1389 }
1390
1391 static ssize_t nvme_identity_len(int hmac, int version, const char *hostnqn,
1392 const char *subsysnqn)
1393 {
1394 ssize_t len;
1395
1396 if (!hostnqn || !subsysnqn) {
1397 errno = EINVAL;
1398 return -1;
1399 }
1400
1401 len = strlen(hostnqn) + strlen(subsysnqn) + 12;
1402 if (version == 1) {
1403 len += 66;
1404 if (hmac == NVME_HMAC_ALG_SHA2_384)
1405 len += 32;
1406 } else if (version > 1) {
1407 errno = EINVAL;
1408 return -1;
1409 }
1410 return len;
1411 }
1412
1413 char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn,
1414 int version, int hmac,
1415 unsigned char *configured_key, int key_len)
1416 {
1417 _cleanup_free_ unsigned char *psk = NULL;
1418 char *identity;
1419 ssize_t identity_len;
1420 int ret = -1;
1421
1422 identity_len = nvme_identity_len(hmac, version, hostnqn, subsysnqn);
1423 if (identity_len < 0) {
1424 errno = EINVAL;
1425 return NULL;
1426 }
1427
1428 identity = malloc(identity_len);
1429 if (!identity) {
1430 errno = ENOMEM;
1431 return NULL;
1432 }
1433
1434 psk = malloc(key_len);
1435 if (!psk) {
1436 errno = ENOMEM;
1437 goto out_free_identity;
1438 }
1439
1440 memset(psk, 0, key_len);
1441 ret = derive_nvme_keys(hostnqn, subsysnqn, identity, version, hmac,
1442 configured_key, psk, key_len, false);
1443 out_free_identity:
1444 if (ret < 0) {
1445 free(identity);
1446 identity = NULL;
1447 }
1448 return identity;
1449 }
1450
1451 char *nvme_generate_tls_key_identity_compat(const char *hostnqn, const char *subsysnqn,
1452 int version, int hmac,
1453 unsigned char *configured_key, int key_len)
1454 {
1455 _cleanup_free_ unsigned char *psk = NULL;
1456 char *identity;
1457 ssize_t identity_len;
1458 int ret = -1;
1459
1460 identity_len = nvme_identity_len(hmac, version, hostnqn, subsysnqn);
1461 if (identity_len < 0) {
1462 errno = EINVAL;
1463 return NULL;
1464 }
1465
1466 identity = malloc(identity_len);
1467 if (!identity) {
1468 errno = ENOMEM;
1469 return NULL;
1470 }
1471
1472 psk = malloc(key_len);
1473 if (!psk) {
1474 errno = ENOMEM;
1475 goto out_free_identity;
1476 }
1477
1478 memset(psk, 0, key_len);
1479 ret = derive_nvme_keys(hostnqn, subsysnqn, identity, version, hmac,
1480 configured_key, psk, key_len, true);
1481 out_free_identity:
1482 if (ret < 0) {
1483 free(identity);
1484 identity = NULL;
1485 }
1486 return identity;
1487 }
1488
1489 #ifdef CONFIG_KEYUTILS
1490 long nvme_lookup_keyring(const char *keyring)
1491 {
1492 key_serial_t keyring_id;
1493
1494 if (!keyring)
1495 keyring = NVME_TLS_DEFAULT_KEYRING;
1496 keyring_id = find_key_by_type_and_desc("keyring", keyring, 0);
1497 if (keyring_id < 0)
1498 return 0;
1499 return keyring_id;
1500 }
1501
1502 char *nvme_describe_key_serial(long key_id)
1503 {
1504 _cleanup_free_ char *str = NULL;
1505 char *last;
1506
1507 if (keyctl_describe_alloc(key_id, &str) < 0)
1508 return NULL;
1509
1510 last = strrchr(str, ';');
1511 if (!last)
1512 return NULL;
1513
1514 last++;
1515 if (strlen(last) == 0)
1516 return NULL;
1517
1518 return strdup(last);
1519 }
1520
1521 long nvme_lookup_key(const char *type, const char *identity)
1522 {
1523 key_serial_t key;
1524
1525 key = keyctl_search(KEY_SPEC_SESSION_KEYRING, type, identity, 0);
1526 if (key < 0)
1527 return 0;
1528 return key;
1529 }
1530
1531 int nvme_set_keyring(long key_id)
1532 {
1533 long err;
1534
1535 if (key_id == 0) {
1536 key_id = nvme_lookup_keyring(NULL);
1537 if (key_id == 0) {
1538 errno = ENOKEY;
1539 return -1;
1540 }
1541 }
1542
1543 err = keyctl_link(key_id, KEY_SPEC_SESSION_KEYRING);
1544 if (err < 0)
1545 return -1;
1546 return 0;
1547 }
1548
1549 unsigned char *nvme_read_key(long keyring_id, long key_id, int *len)
1550 {
1551 void *buffer;
1552 int ret;
1553
1554 ret = nvme_set_keyring(keyring_id);
1555 if (ret < 0) {
1556 errno = -ret;
1557 return NULL;
1558 }
1559 ret = keyctl_read_alloc(key_id, &buffer);
1560 if (ret < 0) {
1561 errno = -ret;
1562 buffer = NULL;
1563 } else
1564 *len = ret;
1565
1566 return buffer;
1567 }
1568
1569 long nvme_update_key(long keyring_id, const char *key_type,
1570 const char *identity, unsigned char *key_data,
1571 int key_len)
1572 {
1573 long key;
1574
1575 key = keyctl_search(keyring_id, key_type, identity, 0);
1576 if (key > 0) {
1577 if (keyctl_revoke(key) < 0)
1578 return 0;
1579 }
1580 key = add_key(key_type, identity,
1581 key_data, key_len, keyring_id);
1582 if (key < 0)
1583 key = 0;
1584 return key;
1585 }
1586
1587 struct __scan_keys_data {
1588 nvme_scan_tls_keys_cb_t cb;
1589 key_serial_t keyring;
1590 void *data;
1591 };
1592
1593 int __scan_keys_cb(key_serial_t parent, key_serial_t key,
1594 char *desc, int desc_len, void *data)
1595 {
1596 struct __scan_keys_data *d = data;
1597 int ver, hmac, uid, gid, perm;
1598 char type, *ptr;
1599
1600 if (desc_len < 6)
1601 return 0;
1602 if (sscanf(desc, "psk;%d;%d;%08x;NVMe%01d%c%02d %*s",
1603 &uid, &gid, &perm, &ver, &type, &hmac) != 6)
1604 return 0;
1605 /* skip key type */
1606 ptr = strchr(desc, ';');
1607 if (!ptr)
1608 return 0;
1609 /* skip key uid */
1610 ptr = strchr(ptr + 1, ';');
1611 if (!ptr)
1612 return 0;
1613 /* skip key gid */
1614 ptr = strchr(ptr + 1, ';');
1615 if (!ptr)
1616 return 0;
1617 /* skip key permissions */
1618 ptr = strchr(ptr + 1, ';');
1619 if (!ptr)
1620 return 0;
1621 /* Only use the key description for the callback */
1622 (d->cb)(d->keyring, key, ptr + 1, strlen(ptr) - 1, d->data);
1623 return 1;
1624 }
1625
1626 int nvme_scan_tls_keys(const char *keyring, nvme_scan_tls_keys_cb_t cb,
1627 void *data)
1628 {
1629 struct __scan_keys_data d;
1630 key_serial_t keyring_id = nvme_lookup_keyring(keyring);
1631 int ret;
1632
1633 if (!keyring_id) {
1634 errno = EINVAL;
1635 return -1;
1636 }
1637 ret = nvme_set_keyring(keyring_id);
1638 if (ret < 0)
1639 return ret;
1640
1641 d.keyring = keyring_id;
1642 d.cb = cb;
1643 d.data = data;
1644 ret = recursive_key_scan(keyring_id, __scan_keys_cb, &d);
1645 return ret;
1646 }
1647
1648 static long __nvme_insert_tls_key(key_serial_t keyring_id, const char *key_type,
1649 const char *hostnqn, const char *subsysnqn,
1650 int version, int hmac,
1651 unsigned char *configured_key, int key_len,
1652 bool compat)
1653 {
1654 _cleanup_free_ unsigned char *psk = NULL;
1655 _cleanup_free_ char *identity = NULL;
1656 ssize_t identity_len;
1657 key_serial_t key;
1658 int ret;
1659
1660 identity_len = nvme_identity_len(hmac, version, hostnqn, subsysnqn);
1661 if (identity_len < 0)
1662 return 0;
1663
1664 identity = malloc(identity_len);
1665 if (!identity) {
1666 errno = ENOMEM;
1667 return 0;
1668 }
1669 memset(identity, 0, identity_len);
1670
1671 psk = malloc(key_len);
1672 if (!psk) {
1673 errno = ENOMEM;
1674 return 0;
1675 }
1676 memset(psk, 0, key_len);
1677 ret = derive_nvme_keys(hostnqn, subsysnqn, identity, version, hmac,
1678 configured_key, psk, key_len, compat);
1679 if (ret != key_len) {
1680 errno = ENOKEY;
1681 return 0;
1682 }
1683
1684 key = nvme_update_key(keyring_id, key_type, identity,
1685 psk, key_len);
1686 return key;
1687 }
1688
1689 long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type,
1690 const char *hostnqn, const char *subsysnqn,
1691 int version, int hmac,
1692 unsigned char *configured_key, int key_len)
1693 {
1694 key_serial_t keyring_id;
1695 int ret;
1696
1697 keyring_id = nvme_lookup_keyring(keyring);
1698 if (keyring_id == 0) {
1699 errno = ENOKEY;
1700 return 0;
1701 }
1702
1703 ret = nvme_set_keyring(keyring_id);
1704 if (ret < 0)
1705 return 0;
1706 return __nvme_insert_tls_key(keyring_id, key_type,
1707 hostnqn, subsysnqn,
1708 version, hmac,
1709 configured_key, key_len, false);
1710 }
1711
1712 long nvme_insert_tls_key_compat(const char *keyring, const char *key_type,
1713 const char *hostnqn, const char *subsysnqn,
1714 int version, int hmac,
1715 unsigned char *configured_key, int key_len)
1716 {
1717 key_serial_t keyring_id;
1718 int ret;
1719
1720 keyring_id = nvme_lookup_keyring(keyring);
1721 if (keyring_id == 0) {
1722 errno = ENOKEY;
1723 return 0;
1724 }
1725
1726 ret = nvme_set_keyring(keyring_id);
1727 if (ret < 0)
1728 return 0;
1729 return __nvme_insert_tls_key(keyring_id, key_type,
1730 hostnqn, subsysnqn,
1731 version, hmac,
1732 configured_key, key_len, true);
1733 }
1734
1735 long nvme_revoke_tls_key(const char *keyring, const char *key_type,
1736 const char *identity)
1737 {
1738 key_serial_t keyring_id;
1739 long key;
1740
1741 keyring_id = nvme_lookup_keyring(keyring);
1742 if (keyring_id == 0) {
1743 errno = ENOKEY;
1744 return 0;
1745 }
1746
1747 key = keyctl_search(keyring_id, key_type, identity, 0);
1748 if (key < 0)
1749 return -1;
1750
1751 return keyctl_revoke(key);
1752 }
1753
1754 static long __nvme_import_tls_key(long keyring_id,
1755 const char *hostnqn, const char *subsysnqn,
1756 const char *identity, const char *key)
1757 {
1758 _cleanup_free_ unsigned char *key_data = NULL;
1759 unsigned char version;
1760 unsigned char hmac;
1761 size_t key_len;
1762
1763 key_data = nvme_import_tls_key_versioned(key, &version,
1764 &hmac, &key_len);
1765 if (!key_data)
1766 return -EINVAL;
1767
1768 if (hmac == NVME_HMAC_ALG_NONE || !identity) {
1769 /*
1770 * This is a configured key (hmac 0) or we don't know the
1771 * identity and so the assumtion is it is also a
1772 * configured key. Derive a new key and load the newly
1773 * created key into the keystore.
1774 */
1775 return __nvme_insert_tls_key(keyring_id, "psk",
1776 hostnqn, subsysnqn,
1777 version, hmac,
1778 key_data, key_len, false);
1779 }
1780
1781 return nvme_update_key(keyring_id, "psk", identity,
1782 key_data, key_len);
1783 }
1784
1785 int __nvme_import_keys_from_config(nvme_host_t h, nvme_ctrl_t c,
1786 long *keyring_id, long *key_id)
1787 {
1788 const char *hostnqn = nvme_host_get_hostnqn(h);
1789 const char *subsysnqn = nvme_ctrl_get_subsysnqn(c);
1790 const char *keyring, *key, *identity;
1791 long kr_id = 0, id = 0;
1792
1793 if (!hostnqn || !subsysnqn) {
1794 nvme_msg(h->r, LOG_ERR, "Invalid NQNs (%s, %s)\n",
1795 hostnqn, subsysnqn);
1796 return -EINVAL;
1797 }
1798
1799 /* If we don't have a key avoid all keyring operations */
1800 key = nvme_ctrl_get_tls_key(c);
1801 if (!key)
1802 goto out;
1803
1804 keyring = nvme_ctrl_get_keyring(c);
1805 if (keyring) {
1806 kr_id = nvme_lookup_keyring(keyring);
1807 if (kr_id == 0)
1808 return -errno;
1809 } else
1810 kr_id = c->cfg.keyring;
1811
1812 /*
1813 * Fallback to the default keyring. Note this will also add the
1814 * keyring to connect command line and to the JSON config output.
1815 * That means we are explicitly selecting the keyring.
1816 */
1817 if (!kr_id) {
1818 kr_id = nvme_lookup_keyring(".nvme");
1819 if (kr_id == 0)
1820 return -errno;
1821 }
1822
1823 if (nvme_set_keyring(kr_id) < 0) {
1824 nvme_msg(h->r, LOG_ERR, "Failed to set keyring\n");
1825 return -errno;
1826 }
1827
1828 identity = nvme_ctrl_get_tls_key_identity(c);
1829 if (identity)
1830 id = nvme_lookup_key("psk", identity);
1831
1832 if (!id)
1833 id = __nvme_import_tls_key(kr_id, hostnqn,
1834 subsysnqn, identity, key);
1835
1836 if (id <= 0) {
1837 nvme_msg(h->r, LOG_ERR, "Failed to insert TLS KEY, error %d\n",
1838 errno);
1839 return -errno;
1840 }
1841
1842 out:
1843 *keyring_id = kr_id;
1844 *key_id = id;
1845
1846 return 0;
1847 }
1848 #else
1849 long nvme_lookup_keyring(const char *keyring)
1850 {
1851 nvme_msg(NULL, LOG_ERR, "key operations not supported; "\
1852 "recompile with keyutils support.\n");
1853 errno = ENOTSUP;
1854 return 0;
1855 }
1856
1857 char *nvme_describe_key_serial(long key_id)
1858 {
1859 nvme_msg(NULL, LOG_ERR, "key operations not supported; "\
1860 "recompile with keyutils support.\n");
1861 errno = ENOTSUP;
1862 return NULL;
1863 }
1864
1865 long nvme_lookup_key(const char *type, const char *identity)
1866 {
1867 nvme_msg(NULL, LOG_ERR, "key operations not supported; "\
1868 "recompile with keyutils support.\n");
1869 errno = ENOTSUP;
1870 return 0;
1871 }
1872
1873 int nvme_set_keyring(long key_id)
1874 {
1875 nvme_msg(NULL, LOG_ERR, "key operations not supported; "\
1876 "recompile with keyutils support.\n");
1877 errno = ENOTSUP;
1878 return -1;
1879 }
1880
1881 unsigned char *nvme_read_key(long keyring_id, long key_id, int *len)
1882 {
1883 errno = ENOTSUP;
1884 return NULL;
1885 }
1886
1887 long nvme_update_key(long keyring_id, const char *key_type,
1888 const char *identity, unsigned char *key_data,
1889 int key_len)
1890 {
1891 errno = ENOTSUP;
1892 return 0;
1893 }
1894
1895 int nvme_scan_tls_keys(const char *keyring, nvme_scan_tls_keys_cb_t cb,
1896 void *data)
1897 {
1898 errno = ENOTSUP;
1899 return -1;
1900 }
1901
1902 long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type,
1903 const char *hostnqn, const char *subsysnqn,
1904 int version, int hmac,
1905 unsigned char *configured_key, int key_len)
1906 {
1907 nvme_msg(NULL, LOG_ERR, "key operations not supported; "
1908 "recompile with keyutils support.\n");
1909 errno = ENOTSUP;
1910 return -1;
1911 }
1912
1913 long nvme_insert_tls_key_compat(const char *keyring, const char *key_type,
1914 const char *hostnqn, const char *subsysnqn,
1915 int version, int hmac,
1916 unsigned char *configured_key, int key_len)
1917 {
1918 nvme_msg(NULL, LOG_ERR, "key operations not supported; "
1919 "recompile with keyutils support.\n");
1920 errno = ENOTSUP;
1921 return -1;
1922 }
1923
1924 long nvme_revoke_tls_key(const char *keyring, const char *key_type,
1925 const char *identity)
1926 {
1927 nvme_msg(NULL, LOG_ERR, "key operations not supported; "
1928 "recompile with keyutils support.\n");
1929 errno = ENOTSUP;
1930 return -1;
1931 }
1932
1933 int __nvme_import_keys_from_config(nvme_host_t h, nvme_ctrl_t c,
1934 long *keyring_id, long *key_id)
1935 {
1936 *keyring_id = 0;
1937 *key_id = 0;
1938
1939 return 0;
1940 }
1941 #endif
1942
1943 long nvme_insert_tls_key(const char *keyring, const char *key_type,
1944 const char *hostnqn, const char *subsysnqn, int hmac,
1945 unsigned char *configured_key, int key_len)
1946 {
1947 return nvme_insert_tls_key_versioned(keyring, key_type,
1948 hostnqn, subsysnqn, 0, hmac,
1949 configured_key, key_len);
1950 }
1951
1952 /*
1953 * PSK Interchange Format
1954 * NVMeTLSkey-<v>:<xx>:<s>:
1955 *
1956 * x: version as one ASCII char
1957 * yy: hmac encoded as two ASCII chars
1958 * 00: no transform ('configured PSK')
1959 * 01: SHA-256
1960 * 02: SHA-384
1961 * s: 32 or 48 bytes binary followed by a CRC-32 of the configured PSK
1962 * (4 bytes) encoded as base64
1963 */
1964 char *nvme_export_tls_key_versioned(unsigned char version, unsigned char hmac,
1965 const unsigned char *key_data,
1966 size_t key_len)
1967 {
1968 unsigned int raw_len, encoded_len, len;
1969 unsigned long crc = crc32(0L, NULL, 0);
1970 unsigned char raw_secret[52];
1971 char *encoded_key;
1972
1973 switch (hmac) {
1974 case NVME_HMAC_ALG_NONE:
1975 if (key_len != 32 && key_len != 48)
1976 goto err_inval;
1977 break;
1978 case NVME_HMAC_ALG_SHA2_256:
1979 if (key_len != 32)
1980 goto err_inval;
1981 break;
1982 case NVME_HMAC_ALG_SHA2_384:
1983 if (key_len != 48)
1984 goto err_inval;
1985 break;
1986 default:
1987 goto err_inval;
1988 }
1989 raw_len = key_len;
1990
1991 memcpy(raw_secret, key_data, raw_len);
1992 crc = crc32(crc, raw_secret, raw_len);
1993 raw_secret[raw_len++] = crc & 0xff;
1994 raw_secret[raw_len++] = (crc >> 8) & 0xff;
1995 raw_secret[raw_len++] = (crc >> 16) & 0xff;
1996 raw_secret[raw_len++] = (crc >> 24) & 0xff;
1997
1998 encoded_len = (raw_len * 2) + 20;
1999 encoded_key = malloc(encoded_len);
2000 if (!encoded_key) {
2001 errno = ENOMEM;
2002 return NULL;
2003 }
2004 memset(encoded_key, 0, encoded_len);
2005 len = sprintf(encoded_key, "NVMeTLSkey-%x:%02x:", version, hmac);
2006 len += base64_encode(raw_secret, raw_len, encoded_key + len);
2007 encoded_key[len++] = ':';
2008 encoded_key[len++] = '\0';
2009
2010 return encoded_key;
2011
2012 err_inval:
2013 errno = EINVAL;
2014 return NULL;
2015
2016 }
2017
2018 char *nvme_export_tls_key(const unsigned char *key_data, int key_len)
2019 {
2020 unsigned char hmac;
2021
2022 if (key_len == 32)
2023 hmac = NVME_HMAC_ALG_SHA2_256;
2024 else
2025 hmac = NVME_HMAC_ALG_SHA2_384;
2026
2027 return nvme_export_tls_key_versioned(1, hmac, key_data, key_len);
2028 }
2029
2030 unsigned char *nvme_import_tls_key_versioned(const char *encoded_key,
2031 unsigned char *version,
2032 unsigned char *hmac,
2033 size_t *key_len)
2034 {
2035 unsigned char decoded_key[128], *key_data;
2036 unsigned int crc = crc32(0L, NULL, 0);
2037 unsigned int key_crc;
2038 int err, _version, _hmac, decoded_len;
2039 size_t len;
2040
2041 if (sscanf(encoded_key, "NVMeTLSkey-%d:%02x:*s",
2042 &_version, &_hmac) != 2) {
2043 errno = EINVAL;
2044 return NULL;
2045 }
2046
2047 if (_version != 1) {
2048 errno = EINVAL;
2049 return NULL;
2050 }
2051 *version = _version;
2052
2053 len = strlen(encoded_key);
2054 switch (_hmac) {
2055 case NVME_HMAC_ALG_NONE:
2056 if (len != 65 && len != 89)
2057 goto err_inval;
2058 break;
2059 case NVME_HMAC_ALG_SHA2_256:
2060 if (len != 65)
2061 goto err_inval;
2062 break;
2063 case NVME_HMAC_ALG_SHA2_384:
2064 if (len != 89)
2065 goto err_inval;
2066 break;
2067 default:
2068 errno = EINVAL;
2069 return NULL;
2070 }
2071 *hmac = _hmac;
2072
2073 err = base64_decode(encoded_key + 16, len - 17, decoded_key);
2074 if (err < 0) {
2075 errno = ENOKEY;
2076 return NULL;
2077 }
2078 decoded_len = err;
2079 decoded_len -= 4;
2080 if (decoded_len != 32 && decoded_len != 48) {
2081 errno = ENOKEY;
2082 return NULL;
2083 }
2084 crc = crc32(crc, decoded_key, decoded_len);
2085 key_crc = ((uint32_t)decoded_key[decoded_len]) |
2086 ((uint32_t)decoded_key[decoded_len + 1] << 8) |
2087 ((uint32_t)decoded_key[decoded_len + 2] << 16) |
2088 ((uint32_t)decoded_key[decoded_len + 3] << 24);
2089 if (key_crc != crc) {
2090 nvme_msg(NULL, LOG_ERR, "CRC mismatch (key %08x, crc %08x)",
2091 key_crc, crc);
2092 errno = ENOKEY;
2093 return NULL;
2094 }
2095
2096 key_data = malloc(decoded_len);
2097 if (!key_data) {
2098 errno = ENOMEM;
2099 return NULL;
2100 }
2101 memcpy(key_data, decoded_key, decoded_len);
2102
2103 *key_len = decoded_len;
2104 return key_data;
2105
2106 err_inval:
2107 errno = EINVAL;
2108 return NULL;
2109 }
2110
2111 unsigned char *nvme_import_tls_key(const char *encoded_key, int *key_len,
2112 unsigned int *hmac)
2113 {
2114 unsigned char version, _hmac;
2115 unsigned char *psk;
2116 size_t len;
2117
2118 psk = nvme_import_tls_key_versioned(encoded_key, &version,
2119 &_hmac, &len);
2120 if (!psk)
2121 return NULL;
2122
2123 *hmac = _hmac;
2124 *key_len = len;
2125 return psk;
2126 }