2020-02-21 06:12:55 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Google Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_HOSTCC
|
|
|
|
#include "mkimage.h"
|
|
|
|
#include <time.h>
|
|
|
|
#else
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-21 06:12:55 +00:00
|
|
|
#include <malloc.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2020-02-21 06:12:55 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
#endif /* !USE_HOSTCC*/
|
2020-04-16 09:30:18 +00:00
|
|
|
#include <fdt_region.h>
|
2020-02-21 06:12:55 +00:00
|
|
|
#include <image.h>
|
|
|
|
#include <u-boot/rsa.h>
|
2021-02-19 18:45:10 +00:00
|
|
|
#include <u-boot/hash-checksum.h>
|
2020-02-21 06:12:55 +00:00
|
|
|
|
|
|
|
#define IMAGE_MAX_HASHED_NODES 100
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fit_region_make_list() - Make a list of image regions
|
|
|
|
*
|
|
|
|
* Given a list of fdt_regions, create a list of image_regions. This is a
|
|
|
|
* simple conversion routine since the FDT and image code use different
|
|
|
|
* structures.
|
|
|
|
*
|
|
|
|
* @fit: FIT image
|
|
|
|
* @fdt_regions: Pointer to FDT regions
|
|
|
|
* @count: Number of FDT regions
|
|
|
|
* @region: Pointer to image regions, which must hold @count records. If
|
|
|
|
* region is NULL, then (except for an SPL build) the array will be
|
|
|
|
* allocated.
|
|
|
|
* @return: Pointer to image regions
|
|
|
|
*/
|
|
|
|
struct image_region *fit_region_make_list(const void *fit,
|
|
|
|
struct fdt_region *fdt_regions,
|
|
|
|
int count,
|
|
|
|
struct image_region *region)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
debug("Hash regions:\n");
|
|
|
|
debug("%10s %10s\n", "Offset", "Size");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use malloc() except in SPL (to save code size). In SPL the caller
|
|
|
|
* must allocate the array.
|
|
|
|
*/
|
2021-09-26 01:43:39 +00:00
|
|
|
if (!IS_ENABLED(CONFIG_SPL_BUILD) && !region)
|
2020-02-21 06:12:55 +00:00
|
|
|
region = calloc(sizeof(*region), count);
|
|
|
|
if (!region)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
debug("%10x %10x\n", fdt_regions[i].offset,
|
|
|
|
fdt_regions[i].size);
|
|
|
|
region[i].data = fit + fdt_regions[i].offset;
|
|
|
|
region[i].size = fdt_regions[i].size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fit_image_setup_verify(struct image_sign_info *info,
|
|
|
|
const void *fit, int noffset,
|
2021-11-12 19:28:10 +00:00
|
|
|
const void *key_blob, int required_keynode,
|
|
|
|
char **err_msgp)
|
2020-02-21 06:12:55 +00:00
|
|
|
{
|
2022-01-14 09:21:17 +00:00
|
|
|
const char *algo_name;
|
2020-02-21 06:12:55 +00:00
|
|
|
const char *padding_name;
|
|
|
|
|
2021-09-26 01:43:39 +00:00
|
|
|
if (fdt_totalsize(fit) > CONFIG_VAL(FIT_SIGNATURE_MAX_SIZE)) {
|
2020-02-21 06:12:55 +00:00
|
|
|
*err_msgp = "Total size too large";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
|
|
|
|
*err_msgp = "Can't get hash algo property";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
padding_name = fdt_getprop(fit, noffset, "padding", NULL);
|
|
|
|
if (!padding_name)
|
|
|
|
padding_name = RSA_DEFAULT_PADDING_NAME;
|
|
|
|
|
|
|
|
memset(info, '\0', sizeof(*info));
|
2020-04-07 15:58:44 +00:00
|
|
|
info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
|
2021-10-18 05:49:03 +00:00
|
|
|
info->fit = fit;
|
2020-02-21 06:12:55 +00:00
|
|
|
info->node_offset = noffset;
|
|
|
|
info->name = algo_name;
|
|
|
|
info->checksum = image_get_checksum_algo(algo_name);
|
|
|
|
info->crypto = image_get_crypto_algo(algo_name);
|
|
|
|
info->padding = image_get_padding_algo(padding_name);
|
2021-11-12 19:28:10 +00:00
|
|
|
info->fdt_blob = key_blob;
|
2020-02-21 06:12:55 +00:00
|
|
|
info->required_keynode = required_keynode;
|
|
|
|
printf("%s:%s", algo_name, info->keyname);
|
|
|
|
|
|
|
|
if (!info->checksum || !info->crypto || !info->padding) {
|
|
|
|
*err_msgp = "Unknown signature algorithm";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fit_image_check_sig(const void *fit, int noffset, const void *data,
|
2021-11-12 19:28:10 +00:00
|
|
|
size_t size, const void *key_blob, int required_keynode,
|
|
|
|
char **err_msgp)
|
2020-02-21 06:12:55 +00:00
|
|
|
{
|
|
|
|
struct image_sign_info info;
|
|
|
|
struct image_region region;
|
|
|
|
uint8_t *fit_value;
|
|
|
|
int fit_value_len;
|
|
|
|
|
|
|
|
*err_msgp = NULL;
|
2021-11-12 19:28:10 +00:00
|
|
|
if (fit_image_setup_verify(&info, fit, noffset, key_blob,
|
|
|
|
required_keynode, err_msgp))
|
2020-02-21 06:12:55 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (fit_image_hash_get_value(fit, noffset, &fit_value,
|
|
|
|
&fit_value_len)) {
|
|
|
|
*err_msgp = "Can't get hash value property";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
region.data = data;
|
|
|
|
region.size = size;
|
|
|
|
|
|
|
|
if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
|
|
|
|
*err_msgp = "Verification failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fit_image_verify_sig(const void *fit, int image_noffset,
|
|
|
|
const char *data, size_t size,
|
2021-11-12 19:28:08 +00:00
|
|
|
const void *key_blob, int key_offset)
|
2020-02-21 06:12:55 +00:00
|
|
|
{
|
|
|
|
int noffset;
|
|
|
|
char *err_msg = "";
|
|
|
|
int verified = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Process all hash subnodes of the component image node */
|
|
|
|
fdt_for_each_subnode(noffset, fit, image_noffset) {
|
|
|
|
const char *name = fit_get_name(fit, noffset, NULL);
|
|
|
|
|
2021-02-16 00:08:06 +00:00
|
|
|
/*
|
|
|
|
* We don't support this since libfdt considers names with the
|
|
|
|
* name root but different @ suffix to be equal
|
|
|
|
*/
|
|
|
|
if (strchr(name, '@')) {
|
|
|
|
err_msg = "Node name contains @";
|
|
|
|
goto error;
|
|
|
|
}
|
2020-02-21 06:12:55 +00:00
|
|
|
if (!strncmp(name, FIT_SIG_NODENAME,
|
|
|
|
strlen(FIT_SIG_NODENAME))) {
|
2021-11-12 19:28:10 +00:00
|
|
|
ret = fit_image_check_sig(fit, noffset, data, size,
|
|
|
|
key_blob, -1, &err_msg);
|
2020-02-21 06:12:55 +00:00
|
|
|
if (ret) {
|
|
|
|
puts("- ");
|
|
|
|
} else {
|
|
|
|
puts("+ ");
|
|
|
|
verified = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
|
|
|
|
err_msg = "Corrupted or truncated tree";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return verified ? 0 : -EPERM;
|
|
|
|
|
|
|
|
error:
|
|
|
|
printf(" error!\n%s for '%s' hash node in '%s' image node\n",
|
|
|
|
err_msg, fit_get_name(fit, noffset, NULL),
|
|
|
|
fit_get_name(fit, image_noffset, NULL));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fit_image_verify_required_sigs(const void *fit, int image_noffset,
|
|
|
|
const char *data, size_t size,
|
2021-11-12 19:28:08 +00:00
|
|
|
const void *key_blob, int *no_sigsp)
|
2020-02-21 06:12:55 +00:00
|
|
|
{
|
|
|
|
int verify_count = 0;
|
|
|
|
int noffset;
|
2021-11-12 19:28:08 +00:00
|
|
|
int key_node;
|
2020-02-21 06:12:55 +00:00
|
|
|
|
|
|
|
/* Work out what we need to verify */
|
|
|
|
*no_sigsp = 1;
|
2021-11-12 19:28:08 +00:00
|
|
|
key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
|
|
|
|
if (key_node < 0) {
|
2020-02-21 06:12:55 +00:00
|
|
|
debug("%s: No signature node found: %s\n", __func__,
|
2021-11-12 19:28:08 +00:00
|
|
|
fdt_strerror(key_node));
|
2020-02-21 06:12:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-12 19:28:08 +00:00
|
|
|
fdt_for_each_subnode(noffset, key_blob, key_node) {
|
2020-02-21 06:12:55 +00:00
|
|
|
const char *required;
|
|
|
|
int ret;
|
|
|
|
|
2021-11-12 19:28:08 +00:00
|
|
|
required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
|
2020-04-07 15:58:44 +00:00
|
|
|
NULL);
|
2020-02-21 06:12:55 +00:00
|
|
|
if (!required || strcmp(required, "image"))
|
|
|
|
continue;
|
|
|
|
ret = fit_image_verify_sig(fit, image_noffset, data, size,
|
2021-11-12 19:28:08 +00:00
|
|
|
key_blob, noffset);
|
2020-02-21 06:12:55 +00:00
|
|
|
if (ret) {
|
|
|
|
printf("Failed to verify required signature '%s'\n",
|
2021-11-12 19:28:08 +00:00
|
|
|
fit_get_name(key_blob, noffset, NULL));
|
2020-02-21 06:12:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
verify_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verify_count)
|
|
|
|
*no_sigsp = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:58:44 +00:00
|
|
|
/**
|
|
|
|
* fit_config_check_sig() - Check the signature of a config
|
|
|
|
*
|
2021-11-12 19:28:09 +00:00
|
|
|
* Here we are looking at a particular signature that needs verification (here
|
|
|
|
* signature-1):
|
|
|
|
*
|
|
|
|
* configurations {
|
|
|
|
* default = "conf-1";
|
|
|
|
* conf-1 {
|
|
|
|
* kernel = "kernel-1";
|
|
|
|
* fdt = "fdt-1";
|
|
|
|
* signature-1 {
|
|
|
|
* algo = "sha1,rsa2048";
|
|
|
|
* value = <...conf 1 signature...>;
|
|
|
|
* };
|
|
|
|
* };
|
|
|
|
*
|
2020-04-07 15:58:44 +00:00
|
|
|
* @fit: FIT to check
|
2021-11-12 19:28:09 +00:00
|
|
|
* @noffset: Offset of the signature node being checked (e.g.
|
|
|
|
* /configurations/conf-1/signature-1)
|
|
|
|
* @conf_noffset: Offset of configuration node (e.g. /configurations/conf-1)
|
2021-11-12 19:28:10 +00:00
|
|
|
* @key_blob: Blob containing the keys to check against
|
2021-11-12 19:28:09 +00:00
|
|
|
* @required_keynode: Offset in @key_blob of the required key node,
|
2020-04-07 15:58:44 +00:00
|
|
|
* if any. If this is given, then the configuration wil not
|
|
|
|
* pass verification unless that key is used. If this is
|
|
|
|
* -1 then any signature will do.
|
|
|
|
* @err_msgp: In the event of an error, this will be pointed to a
|
|
|
|
* help error string to display to the user.
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if all verified ok, <0 on error
|
2020-04-07 15:58:44 +00:00
|
|
|
*/
|
2021-11-12 19:28:09 +00:00
|
|
|
static int fit_config_check_sig(const void *fit, int noffset, int conf_noffset,
|
2021-11-12 19:28:10 +00:00
|
|
|
const void *key_blob, int required_keynode,
|
|
|
|
char **err_msgp)
|
2020-02-21 06:12:55 +00:00
|
|
|
{
|
2021-04-20 18:19:44 +00:00
|
|
|
static char * const exc_prop[] = {
|
|
|
|
"data",
|
|
|
|
"data-size",
|
|
|
|
"data-position",
|
|
|
|
"data-offset"
|
|
|
|
};
|
|
|
|
|
2020-02-21 06:12:55 +00:00
|
|
|
const char *prop, *end, *name;
|
|
|
|
struct image_sign_info info;
|
|
|
|
const uint32_t *strings;
|
2020-04-07 15:58:44 +00:00
|
|
|
const char *config_name;
|
2020-02-21 06:12:55 +00:00
|
|
|
uint8_t *fit_value;
|
|
|
|
int fit_value_len;
|
2020-04-07 15:58:44 +00:00
|
|
|
bool found_config;
|
2020-02-21 06:12:55 +00:00
|
|
|
int max_regions;
|
|
|
|
int i, prop_len;
|
|
|
|
char path[200];
|
|
|
|
int count;
|
|
|
|
|
2020-04-07 15:58:44 +00:00
|
|
|
config_name = fit_get_name(fit, conf_noffset, NULL);
|
2021-11-12 19:28:10 +00:00
|
|
|
debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, key_blob,
|
2020-02-21 06:12:55 +00:00
|
|
|
fit_get_name(fit, noffset, NULL),
|
2021-11-12 19:28:10 +00:00
|
|
|
fit_get_name(key_blob, required_keynode, NULL));
|
2020-02-21 06:12:55 +00:00
|
|
|
*err_msgp = NULL;
|
2021-11-12 19:28:10 +00:00
|
|
|
if (fit_image_setup_verify(&info, fit, noffset, key_blob,
|
|
|
|
required_keynode, err_msgp))
|
2020-02-21 06:12:55 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (fit_image_hash_get_value(fit, noffset, &fit_value,
|
|
|
|
&fit_value_len)) {
|
|
|
|
*err_msgp = "Can't get hash value property";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count the number of strings in the property */
|
|
|
|
prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
|
|
|
|
end = prop ? prop + prop_len : prop;
|
|
|
|
for (name = prop, count = 0; name < end; name++)
|
|
|
|
if (!*name)
|
|
|
|
count++;
|
|
|
|
if (!count) {
|
|
|
|
*err_msgp = "Can't get hashed-nodes property";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
|
|
|
|
*err_msgp = "hashed-nodes property must be null-terminated";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a sanity check here since we are using the stack */
|
|
|
|
if (count > IMAGE_MAX_HASHED_NODES) {
|
|
|
|
*err_msgp = "Number of hashed nodes exceeds maximum";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a list of node names from those strings */
|
|
|
|
char *node_inc[count];
|
|
|
|
|
|
|
|
debug("Hash nodes (%d):\n", count);
|
2020-04-07 15:58:44 +00:00
|
|
|
found_config = false;
|
2020-02-21 06:12:55 +00:00
|
|
|
for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
|
|
|
|
debug(" '%s'\n", name);
|
|
|
|
node_inc[i] = (char *)name;
|
2020-04-07 15:58:44 +00:00
|
|
|
if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
|
|
|
|
name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
|
|
|
|
!strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
|
|
|
|
debug(" (found config node %s)", config_name);
|
|
|
|
found_config = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found_config) {
|
|
|
|
*err_msgp = "Selected config not in hashed nodes";
|
|
|
|
return -1;
|
2020-02-21 06:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each node can generate one region for each sub-node. Allow for
|
|
|
|
* 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
|
|
|
|
*/
|
|
|
|
max_regions = 20 + count * 7;
|
|
|
|
struct fdt_region fdt_regions[max_regions];
|
|
|
|
|
|
|
|
/* Get a list of regions to hash */
|
|
|
|
count = fdt_find_regions(fit, node_inc, count,
|
|
|
|
exc_prop, ARRAY_SIZE(exc_prop),
|
|
|
|
fdt_regions, max_regions - 1,
|
|
|
|
path, sizeof(path), 0);
|
|
|
|
if (count < 0) {
|
|
|
|
*err_msgp = "Failed to hash configuration";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (count == 0) {
|
|
|
|
*err_msgp = "No data to hash";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (count >= max_regions - 1) {
|
|
|
|
*err_msgp = "Too many hash regions";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the strings */
|
|
|
|
strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
|
|
|
|
if (strings) {
|
|
|
|
/*
|
|
|
|
* The strings region offset must be a static 0x0.
|
|
|
|
* This is set in tool/image-host.c
|
|
|
|
*/
|
|
|
|
fdt_regions[count].offset = fdt_off_dt_strings(fit);
|
|
|
|
fdt_regions[count].size = fdt32_to_cpu(strings[1]);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the region list on the stack */
|
|
|
|
struct image_region region[count];
|
|
|
|
|
|
|
|
fit_region_make_list(fit, fdt_regions, count, region);
|
|
|
|
if (info.crypto->verify(&info, region, count, fit_value,
|
|
|
|
fit_value_len)) {
|
|
|
|
*err_msgp = "Verification failed";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-12 19:28:08 +00:00
|
|
|
/**
|
|
|
|
* fit_config_verify_key() - Verify that a configuration is signed with a key
|
|
|
|
*
|
|
|
|
* Here we are looking at a particular configuration that needs verification:
|
|
|
|
*
|
|
|
|
* configurations {
|
|
|
|
* default = "conf-1";
|
|
|
|
* conf-1 {
|
|
|
|
* kernel = "kernel-1";
|
|
|
|
* fdt = "fdt-1";
|
|
|
|
* signature-1 {
|
|
|
|
* algo = "sha1,rsa2048";
|
|
|
|
* value = <...conf 1 signature...>;
|
|
|
|
* };
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* We must check each of the signature subnodes of conf-1. Hopefully one of them
|
|
|
|
* will match the key at key_offset.
|
|
|
|
*
|
|
|
|
* @fit: FIT to check
|
|
|
|
* @conf_noffset: Offset of the configuration node to check (e.g.
|
|
|
|
* /configurations/conf-1)
|
|
|
|
* @key_blob: Blob containing the keys to check against
|
|
|
|
* @key_offset: Offset of the key to check within @key_blob
|
|
|
|
* @return 0 if OK, -EPERM if any signatures did not verify, or the
|
|
|
|
* configuration node has an invalid name
|
|
|
|
*/
|
|
|
|
static int fit_config_verify_key(const void *fit, int conf_noffset,
|
|
|
|
const void *key_blob, int key_offset)
|
2020-02-21 06:12:55 +00:00
|
|
|
{
|
|
|
|
int noffset;
|
2021-01-11 14:46:58 +00:00
|
|
|
char *err_msg = "No 'signature' subnode found";
|
2020-02-21 06:12:55 +00:00
|
|
|
int verified = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Process all hash subnodes of the component conf node */
|
|
|
|
fdt_for_each_subnode(noffset, fit, conf_noffset) {
|
|
|
|
const char *name = fit_get_name(fit, noffset, NULL);
|
|
|
|
|
|
|
|
if (!strncmp(name, FIT_SIG_NODENAME,
|
|
|
|
strlen(FIT_SIG_NODENAME))) {
|
2021-11-12 19:28:09 +00:00
|
|
|
ret = fit_config_check_sig(fit, noffset, conf_noffset,
|
2021-11-12 19:28:10 +00:00
|
|
|
key_blob, key_offset,
|
|
|
|
&err_msg);
|
2020-02-21 06:12:55 +00:00
|
|
|
if (ret) {
|
|
|
|
puts("- ");
|
|
|
|
} else {
|
|
|
|
puts("+ ");
|
|
|
|
verified = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
|
|
|
|
err_msg = "Corrupted or truncated tree";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:58:44 +00:00
|
|
|
if (verified)
|
|
|
|
return 0;
|
2020-02-21 06:12:55 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
printf(" error!\n%s for '%s' hash node in '%s' config node\n",
|
|
|
|
err_msg, fit_get_name(fit, noffset, NULL),
|
|
|
|
fit_get_name(fit, conf_noffset, NULL));
|
2020-04-07 15:58:44 +00:00
|
|
|
return -EPERM;
|
2020-02-21 06:12:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 19:28:08 +00:00
|
|
|
/**
|
|
|
|
* fit_config_verify_required_keys() - verify any required signatures for config
|
|
|
|
*
|
|
|
|
* This looks through all the signatures we expect and verifies that at least
|
|
|
|
* all the required ones are valid signatures for the configuration
|
|
|
|
*
|
|
|
|
* @fit: FIT to check
|
|
|
|
* @conf_noffset: Offset of the configuration node to check (e.g.
|
|
|
|
* /configurations/conf-1)
|
|
|
|
* @key_blob: Blob containing the keys to check against
|
|
|
|
* @return 0 if OK, -EPERM if any signatures did not verify, or the
|
|
|
|
* configuration node has an invalid name
|
|
|
|
*/
|
|
|
|
static int fit_config_verify_required_keys(const void *fit, int conf_noffset,
|
|
|
|
const void *key_blob)
|
2020-02-21 06:12:55 +00:00
|
|
|
{
|
2021-02-16 00:08:06 +00:00
|
|
|
const char *name = fit_get_name(fit, conf_noffset, NULL);
|
2020-02-21 06:12:55 +00:00
|
|
|
int noffset;
|
2021-11-12 19:28:08 +00:00
|
|
|
int key_node;
|
2020-08-17 06:01:09 +00:00
|
|
|
int verified = 0;
|
|
|
|
int reqd_sigs = 0;
|
|
|
|
bool reqd_policy_all = true;
|
|
|
|
const char *reqd_mode;
|
2020-02-21 06:12:55 +00:00
|
|
|
|
2021-02-16 00:08:06 +00:00
|
|
|
/*
|
|
|
|
* We don't support this since libfdt considers names with the
|
|
|
|
* name root but different @ suffix to be equal
|
|
|
|
*/
|
|
|
|
if (strchr(name, '@')) {
|
|
|
|
printf("Configuration node '%s' contains '@'\n", name);
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
2020-02-21 06:12:55 +00:00
|
|
|
/* Work out what we need to verify */
|
2021-11-12 19:28:08 +00:00
|
|
|
key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
|
|
|
|
if (key_node < 0) {
|
2020-02-21 06:12:55 +00:00
|
|
|
debug("%s: No signature node found: %s\n", __func__,
|
2021-11-12 19:28:08 +00:00
|
|
|
fdt_strerror(key_node));
|
2020-02-21 06:12:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-17 06:01:09 +00:00
|
|
|
/* Get required-mode policy property from DTB */
|
2021-11-12 19:28:08 +00:00
|
|
|
reqd_mode = fdt_getprop(key_blob, key_node, "required-mode", NULL);
|
2020-08-17 06:01:09 +00:00
|
|
|
if (reqd_mode && !strcmp(reqd_mode, "any"))
|
|
|
|
reqd_policy_all = false;
|
|
|
|
|
|
|
|
debug("%s: required-mode policy set to '%s'\n", __func__,
|
|
|
|
reqd_policy_all ? "all" : "any");
|
|
|
|
|
2021-11-12 19:28:08 +00:00
|
|
|
/*
|
|
|
|
* The algorithm here is a little convoluted due to how we want it to
|
|
|
|
* work. Here we work through each of the signature nodes in the
|
|
|
|
* public-key area. These are in the U-Boot control devicetree. Each
|
|
|
|
* node was created by signing a configuration, so we check if it is
|
|
|
|
* 'required' and if so, request that it be verified.
|
|
|
|
*/
|
|
|
|
fdt_for_each_subnode(noffset, key_blob, key_node) {
|
2020-02-21 06:12:55 +00:00
|
|
|
const char *required;
|
|
|
|
int ret;
|
|
|
|
|
2021-11-12 19:28:08 +00:00
|
|
|
required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
|
2020-04-07 15:58:44 +00:00
|
|
|
NULL);
|
2020-02-21 06:12:55 +00:00
|
|
|
if (!required || strcmp(required, "conf"))
|
|
|
|
continue;
|
2020-08-17 06:01:09 +00:00
|
|
|
|
|
|
|
reqd_sigs++;
|
|
|
|
|
2021-11-12 19:28:08 +00:00
|
|
|
ret = fit_config_verify_key(fit, conf_noffset, key_blob,
|
2020-02-21 06:12:55 +00:00
|
|
|
noffset);
|
|
|
|
if (ret) {
|
2020-08-17 06:01:09 +00:00
|
|
|
if (reqd_policy_all) {
|
|
|
|
printf("Failed to verify required signature '%s'\n",
|
2021-11-12 19:28:08 +00:00
|
|
|
fit_get_name(key_blob, noffset, NULL));
|
2020-08-17 06:01:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
verified++;
|
|
|
|
if (!reqd_policy_all)
|
|
|
|
break;
|
2020-02-21 06:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 06:01:09 +00:00
|
|
|
if (reqd_sigs && !verified) {
|
|
|
|
printf("Failed to verify 'any' of the required signature(s)\n");
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
2020-02-21 06:12:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fit_config_verify(const void *fit, int conf_noffset)
|
|
|
|
{
|
2021-11-12 19:28:08 +00:00
|
|
|
return fit_config_verify_required_keys(fit, conf_noffset,
|
2020-02-21 06:12:55 +00:00
|
|
|
gd_fdt_blob());
|
|
|
|
}
|