| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/** |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (c) 2024 Daniel Wagner, SUSE LLC |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "nvme/linux.h" |
| 8 |
#include "nvme/tree.h" |
| 9 |
#include <string.h> |
| 10 |
#include <stdbool.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <errno.h> |
| 13 |
|
| 14 |
#include <libnvme.h> |
| 15 |
|
| 16 |
static bool import_export_key(nvme_ctrl_t c) |
| 17 |
{ |
| 18 |
unsigned char version, hmac, *key; |
| 19 |
char *encoded_key; |
| 20 |
size_t len; |
| 21 |
|
| 22 |
key = nvme_import_tls_key_versioned(nvme_ctrl_get_tls_key(c), |
| 23 |
&version, &hmac, &len); |
| 24 |
if (!key) { |
| 25 |
printf("ERROR: nvme_import_tls_key_versioned failed with %d\n", |
| 26 |
errno); |
| 27 |
return false; |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
encoded_key = nvme_export_tls_key_versioned(version, hmac, key, len); |
| 32 |
free(key); |
| 33 |
if (!encoded_key) { |
| 34 |
printf("ERROR: nvme_export_tls_key_versioned failed with %d\n", |
| 35 |
errno); |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
nvme_ctrl_set_tls_key(c, encoded_key); |
| 40 |
|
| 41 |
free(encoded_key); |
| 42 |
|
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
static bool psk_json_test(char *file) |
| 47 |
{ |
| 48 |
bool pass = false; |
| 49 |
nvme_root_t r; |
| 50 |
nvme_host_t h; |
| 51 |
nvme_subsystem_t s; |
| 52 |
nvme_ctrl_t c; |
| 53 |
int err; |
| 54 |
|
| 55 |
r = nvme_create_root(stderr, LOG_ERR); |
| 56 |
if (!r) |
| 57 |
return false; |
| 58 |
|
| 59 |
err = nvme_read_config(r, file); |
| 60 |
if (err) |
| 61 |
goto out; |
| 62 |
|
| 63 |
|
| 64 |
nvme_for_each_host(r, h) |
| 65 |
nvme_for_each_subsystem(h, s) |
| 66 |
nvme_subsystem_for_each_ctrl(s, c) |
| 67 |
if (!import_export_key(c)) |
| 68 |
goto out; |
| 69 |
|
| 70 |
err = nvme_dump_config(r); |
| 71 |
if (err) |
| 72 |
goto out; |
| 73 |
|
| 74 |
pass = true; |
| 75 |
|
| 76 |
out: |
| 77 |
nvme_free_tree(r); |
| 78 |
return pass; |
| 79 |
} |
| 80 |
|
| 81 |
int main(int argc, char *argv[]) |
| 82 |
{ |
| 83 |
bool pass; |
| 84 |
|
| 85 |
pass = psk_json_test(argv[1]); |
| 86 |
fflush(stdout); |
| 87 |
|
| 88 |
exit(pass ? EXIT_SUCCESS : EXIT_FAILURE); |
| 89 |
} |