Blame mesh/keyring.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2019  Intel Corporation. All rights reserved.
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This library is free software; you can redistribute it and/or
Packit 34410b
 *  modify it under the terms of the GNU Lesser General Public
Packit 34410b
 *  License as published by the Free Software Foundation; either
Packit 34410b
 *  version 2.1 of the License, or (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This library is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 34410b
 *  Lesser General Public License for more details.
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#define _GNU_SOURCE
Packit 34410b
#include <fcntl.h>
Packit 34410b
#include <dirent.h>
Packit 34410b
#include <limits.h>
Packit 34410b
#include <stdio.h>
Packit 34410b
#include <unistd.h>
Packit 34410b
#include <dirent.h>
Packit 34410b
Packit 34410b
#include <sys/stat.h>
Packit 34410b
Packit 34410b
#include <ell/ell.h>
Packit 34410b
Packit 34410b
#include "mesh/mesh-defs.h"
Packit 34410b
Packit 34410b
#include "mesh/node.h"
Packit 34410b
#include "mesh/keyring.h"
Packit 34410b
Packit 34410b
const char *dev_key_dir = "/dev_keys";
Packit 34410b
const char *app_key_dir = "/app_keys";
Packit 34410b
const char *net_key_dir = "/net_keys";
Packit 34410b
Packit 34410b
bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,
Packit 34410b
						struct keyring_net_key *key)
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
	bool result = false;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	if (!node || !key)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
Packit 34410b
	if (strlen(node_path) + strlen(net_key_dir) + 1 + 3 >= PATH_MAX)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s", node_path, net_key_dir);
Packit 34410b
	mkdir(key_file, 0755);
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, net_key_dir,
Packit 34410b
								net_idx);
Packit 34410b
	l_debug("Put Net Key %s", key_file);
Packit 34410b
Packit 34410b
	fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Packit 34410b
	if (fd >= 0) {
Packit 34410b
		if (write(fd, key, sizeof(*key)) == sizeof(*key))
Packit 34410b
			result = true;
Packit 34410b
Packit 34410b
		close(fd);
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return result;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
Packit 34410b
				uint16_t net_idx, struct keyring_app_key *key)
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
	bool result = false;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	if (!node || !key)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
Packit 34410b
	if (strlen(node_path) + strlen(app_key_dir) + 1 + 3 >= PATH_MAX)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s", node_path, app_key_dir);
Packit 34410b
	mkdir(key_file, 0755);
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, app_key_dir,
Packit 34410b
								app_idx);
Packit 34410b
	l_debug("Put App Key %s", key_file);
Packit 34410b
Packit 34410b
	fd = open(key_file, O_RDWR);
Packit 34410b
	if (fd >= 0) {
Packit 34410b
		struct keyring_app_key old_key;
Packit 34410b
Packit 34410b
		if (read(fd, &old_key, sizeof(old_key)) == sizeof(old_key)) {
Packit 34410b
			if (old_key.net_idx != net_idx) {
Packit 34410b
				close(fd);
Packit 34410b
				return false;
Packit 34410b
			}
Packit 34410b
		}
Packit 34410b
Packit 34410b
		lseek(fd, 0, SEEK_SET);
Packit 34410b
	} else
Packit 34410b
		fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC,
Packit 34410b
							S_IRUSR | S_IWUSR);
Packit 34410b
Packit 34410b
	if (fd >= 0) {
Packit 34410b
		if (write(fd, key, sizeof(*key)) == sizeof(*key))
Packit 34410b
			result = true;
Packit 34410b
Packit 34410b
		close(fd);
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return result;
Packit 34410b
}
Packit 34410b
Packit 34410b
static void finalize(const char *fpath, uint16_t net_idx)
Packit 34410b
{
Packit 34410b
	struct keyring_app_key key;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	fd = open(fpath, O_RDWR);
Packit 34410b
Packit 34410b
	if (fd < 0)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	if (read(fd, &key, sizeof(key)) != sizeof(key) ||
Packit 34410b
						key.net_idx != net_idx)
Packit 34410b
		goto done;
Packit 34410b
Packit 34410b
	l_debug("Finalize %s", fpath);
Packit 34410b
	memcpy(key.old_key, key.new_key, 16);
Packit 34410b
	lseek(fd, 0, SEEK_SET);
Packit 34410b
	write(fd, &key, sizeof(key));
Packit 34410b
Packit 34410b
done:
Packit 34410b
	close(fd);
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_finalize_app_keys(struct mesh_node *node, uint16_t net_idx)
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_dir[PATH_MAX];
Packit 34410b
	DIR *dir;
Packit 34410b
	struct dirent *entry;
Packit 34410b
Packit 34410b
	if (!node)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
Packit 34410b
	if (strlen(node_path) + strlen(app_key_dir) + 1 >= PATH_MAX)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	snprintf(key_dir, PATH_MAX, "%s%s", node_path, app_key_dir);
Packit 34410b
	dir = opendir(key_dir);
Packit 34410b
	if (!dir) {
Packit 34410b
		l_error("Failed to App Key storage directory: %s", key_dir);
Packit 34410b
		return false;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	while ((entry = readdir(dir)) != NULL) {
Packit 34410b
		/* AppKeys are stored in regular files */
Packit 34410b
		if (entry->d_type == DT_REG)
Packit 34410b
			finalize(entry->d_name, net_idx);
Packit 34410b
	}
Packit 34410b
Packit 34410b
	closedir(dir);
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,
Packit 34410b
					uint8_t count, uint8_t dev_key[16])
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
	bool result = true;
Packit 34410b
	int fd, i;
Packit 34410b
Packit 34410b
	if (!IS_UNICAST_RANGE(unicast, count))
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	if (!node)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
Packit 34410b
	if (strlen(node_path) + strlen(dev_key_dir) + 1 + 4 >= PATH_MAX)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s", node_path, dev_key_dir);
Packit 34410b
	mkdir(key_file, 0755);
Packit 34410b
Packit 34410b
	for (i = 0; i < count; i++) {
Packit 34410b
		snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path,
Packit 34410b
						dev_key_dir, unicast + i);
Packit 34410b
		l_debug("Put Dev Key %s", key_file);
Packit 34410b
Packit 34410b
		fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC,
Packit 34410b
							S_IRUSR | S_IWUSR);
Packit 34410b
		if (fd >= 0) {
Packit 34410b
			if (write(fd, dev_key, 16) != 16)
Packit 34410b
				result = false;
Packit 34410b
Packit 34410b
			close(fd);
Packit 34410b
		} else
Packit 34410b
			result = false;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return result;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_get_net_key(struct mesh_node *node, uint16_t net_idx,
Packit 34410b
						struct keyring_net_key *key)
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
	bool result = false;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	if (!node || !key)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, net_key_dir,
Packit 34410b
								net_idx);
Packit 34410b
Packit 34410b
	fd = open(key_file, O_RDONLY);
Packit 34410b
	if (fd >= 0) {
Packit 34410b
		if (read(fd, key, sizeof(*key)) == sizeof(*key))
Packit 34410b
			result = true;
Packit 34410b
Packit 34410b
		close(fd);
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return result;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_get_app_key(struct mesh_node *node, uint16_t app_idx,
Packit 34410b
						struct keyring_app_key *key)
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
	bool result = false;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	if (!node || !key)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, app_key_dir,
Packit 34410b
								app_idx);
Packit 34410b
Packit 34410b
	fd = open(key_file, O_RDONLY);
Packit 34410b
	if (fd >= 0) {
Packit 34410b
		if (read(fd, key, sizeof(*key)) == sizeof(*key))
Packit 34410b
			result = true;
Packit 34410b
Packit 34410b
		close(fd);
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return result;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_get_remote_dev_key(struct mesh_node *node, uint16_t unicast,
Packit 34410b
							uint8_t dev_key[16])
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
	bool result = false;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	if (!IS_UNICAST(unicast))
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	if (!node)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path, dev_key_dir,
Packit 34410b
								unicast);
Packit 34410b
Packit 34410b
	fd = open(key_file, O_RDONLY);
Packit 34410b
	if (fd >= 0) {
Packit 34410b
		if (read(fd, dev_key, 16) == 16)
Packit 34410b
			result = true;
Packit 34410b
Packit 34410b
		close(fd);
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return result;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_del_net_key(struct mesh_node *node, uint16_t net_idx)
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
Packit 34410b
	if (!node)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, net_key_dir,
Packit 34410b
								net_idx);
Packit 34410b
	l_debug("RM Net Key %s", key_file);
Packit 34410b
	remove(key_file);
Packit 34410b
Packit 34410b
	/* TODO: See if it is easiest to delete all bound App keys here */
Packit 34410b
	/* TODO: see nftw() */
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_del_app_key(struct mesh_node *node, uint16_t app_idx)
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
Packit 34410b
	if (!node)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, app_key_dir,
Packit 34410b
								app_idx);
Packit 34410b
	l_debug("RM App Key %s", key_file);
Packit 34410b
	remove(key_file);
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}
Packit 34410b
Packit 34410b
bool keyring_del_remote_dev_key(struct mesh_node *node, uint16_t unicast,
Packit 34410b
								uint8_t count)
Packit 34410b
{
Packit 34410b
	const char *node_path;
Packit 34410b
	char key_file[PATH_MAX];
Packit 34410b
	int i;
Packit 34410b
Packit 34410b
	if (!IS_UNICAST_RANGE(unicast, count))
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	if (!node)
Packit 34410b
		return false;
Packit 34410b
Packit 34410b
	node_path = node_get_storage_dir(node);
Packit 34410b
Packit 34410b
	for (i = 0; i < count; i++) {
Packit 34410b
		snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path,
Packit 34410b
						dev_key_dir, unicast + i);
Packit 34410b
		l_debug("RM Dev Key %s", key_file);
Packit 34410b
		remove(key_file);
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}