summaryrefslogtreecommitdiffstats
path: root/example/keys.c
blob: 958e1d799ec17ca465657c5c0790a3671ce30be0 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <inttypes.h>

#include <dect/libdect.h>
#include <dect/auth.h>
#include "common.h"

static FILE *dect_keyfile_open(const char *mode)
{
	char name[PATH_MAX];

	snprintf(name, sizeof(name), "%s/%s", getenv("HOME"), "dect.keys");
	return fopen(name, mode);
}

int dect_write_uak(const struct dect_ipui *ipui,
		   const uint8_t uak[DECT_AUTH_KEY_LEN])
{
	unsigned int i;
	FILE *f;

	f = dect_keyfile_open("w");
	if (f == NULL)
		return -1;

	fprintf(f, "N|%04x|%05x|", ipui->pun.n.ipei.emc, ipui->pun.n.ipei.psn);
	for (i = 0; i < DECT_AUTH_KEY_LEN; i++)
		fprintf(f, "%.2x", uak[i]);
	fprintf(f, "\n");

	fclose(f);
	return 0;
}

int dect_read_uak(const struct dect_ipui *ipui, uint8_t _uak[DECT_AUTH_KEY_LEN])
{
	struct dect_ipui ripui;
	uint8_t uak[DECT_AUTH_KEY_LEN];
	unsigned int i;
	FILE *f;

	f = dect_keyfile_open("r");
	if (f == NULL)
		goto err;

	memset(&ripui, 0, sizeof(ripui));
	ripui.put = DECT_IPUI_N;

	if (fscanf(f, "N|%04hx|%05x|",
		   &ripui.pun.n.ipei.emc,
		   &ripui.pun.n.ipei.psn) != 2)
		goto err;

	for (i = 0; i < DECT_AUTH_KEY_LEN; i++) {
		if (fscanf(f, "%02hhx", &uak[i]) != 1)
			goto err;
	}

	if (dect_ipui_cmp(ipui, &ripui))
		goto err;

	memcpy(_uak, uak, DECT_AUTH_KEY_LEN);

	fclose(f);
	return 0;

err:
	fprintf(stderr, "Could not find UAK for IPUI N %4x %5x, use "
			"'pp-access-rights' to allocate a new one\n",
		ipui->pun.n.ipei.emc, ipui->pun.n.ipei.psn);
	return -1;
}