File: psk-json.c

package info (click to toggle)
libnvme 1.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,016 kB
  • sloc: ansic: 35,812; perl: 1,834; sh: 475; python: 194; cpp: 64; makefile: 55
file content (89 lines) | stat: -rw-r--r-- 1,558 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: LGPL-2.1-or-later
/**
 * This file is part of libnvme.
 * Copyright (c) 2024 Daniel Wagner, SUSE LLC
 */

#include "nvme/linux.h"
#include "nvme/tree.h"
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>

#include <libnvme.h>

static bool import_export_key(nvme_ctrl_t c)
{
	unsigned char version, hmac, *key;
	char *encoded_key;
	size_t len;

	key = nvme_import_tls_key_versioned(nvme_ctrl_get_tls_key(c),
					    &version, &hmac, &len);
	if (!key) {
		printf("ERROR: nvme_import_tls_key_versioned failed with %d\n",
		       errno);
		return false;

	}

	encoded_key = nvme_export_tls_key_versioned(version, hmac, key, len);
	free(key);
	if (!encoded_key) {
		printf("ERROR: nvme_export_tls_key_versioned failed with %d\n",
		       errno);
		return false;
	}

	nvme_ctrl_set_tls_key(c, encoded_key);

	free(encoded_key);

	return true;
}

static bool psk_json_test(char *file)
{
	bool pass = false;
	nvme_root_t r;
	nvme_host_t h;
	nvme_subsystem_t s;
	nvme_ctrl_t c;
	int err;

	r = nvme_create_root(stderr, LOG_ERR);
	if (!r)
		return false;

	err = nvme_read_config(r, file);
	if (err)
		goto out;


	nvme_for_each_host(r, h)
		nvme_for_each_subsystem(h, s)
			nvme_subsystem_for_each_ctrl(s, c)
				if (!import_export_key(c))
					goto out;

	err = nvme_dump_config(r);
	if (err)
		goto out;

	pass = true;

out:
	nvme_free_tree(r);
	return pass;
}

int main(int argc, char *argv[])
{
	bool pass;

	pass = psk_json_test(argv[1]);
	fflush(stdout);

	exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
}