blob: f23e9d5d0b0fad7fd5112cb0d1fcebb5dc34247a [file] [log] [blame]
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2013, Google Inc.
4 */
5
6#ifdef USE_HOSTCC
7#include "mkimage.h"
8#include <time.h>
9#else
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090011#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090013DECLARE_GLOBAL_DATA_PTR;
14#endif /* !USE_HOSTCC*/
Masahiro Yamada6dd10522020-04-16 18:30:18 +090015#include <fdt_region.h>
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090016#include <image.h>
17#include <u-boot/rsa.h>
Alexandru Gagniucdb182c42021-02-19 12:45:10 -060018#include <u-boot/hash-checksum.h>
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090019
20#define IMAGE_MAX_HASHED_NODES 100
21
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090022/**
23 * fit_region_make_list() - Make a list of image regions
24 *
25 * Given a list of fdt_regions, create a list of image_regions. This is a
26 * simple conversion routine since the FDT and image code use different
27 * structures.
28 *
29 * @fit: FIT image
30 * @fdt_regions: Pointer to FDT regions
31 * @count: Number of FDT regions
32 * @region: Pointer to image regions, which must hold @count records. If
33 * region is NULL, then (except for an SPL build) the array will be
34 * allocated.
35 * @return: Pointer to image regions
36 */
37struct image_region *fit_region_make_list(const void *fit,
38 struct fdt_region *fdt_regions,
39 int count,
40 struct image_region *region)
41{
42 int i;
43
44 debug("Hash regions:\n");
45 debug("%10s %10s\n", "Offset", "Size");
46
47 /*
48 * Use malloc() except in SPL (to save code size). In SPL the caller
49 * must allocate the array.
50 */
Simon Glass0e84d962024-09-29 19:49:50 -060051 if (!IS_ENABLED(CONFIG_XPL_BUILD) && !region)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090052 region = calloc(sizeof(*region), count);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090053 if (!region)
54 return NULL;
55 for (i = 0; i < count; i++) {
56 debug("%10x %10x\n", fdt_regions[i].offset,
57 fdt_regions[i].size);
58 region[i].data = fit + fdt_regions[i].offset;
59 region[i].size = fdt_regions[i].size;
60 }
61
62 return region;
63}
64
65static int fit_image_setup_verify(struct image_sign_info *info,
66 const void *fit, int noffset,
Simon Glasse10e2ee2021-11-12 12:28:10 -070067 const void *key_blob, int required_keynode,
68 char **err_msgp)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090069{
Jan Kiszka27beec22022-01-14 10:21:17 +010070 const char *algo_name;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090071 const char *padding_name;
72
Simon Glassc0cabbc2021-09-25 19:43:39 -060073 if (fdt_totalsize(fit) > CONFIG_VAL(FIT_SIGNATURE_MAX_SIZE)) {
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090074 *err_msgp = "Total size too large";
75 return 1;
76 }
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090077 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
78 *err_msgp = "Can't get hash algo property";
79 return -1;
80 }
81
82 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
83 if (!padding_name)
84 padding_name = RSA_DEFAULT_PADDING_NAME;
85
86 memset(info, '\0', sizeof(*info));
Tom Rini7357a352020-04-07 11:58:44 -040087 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Hannu Lounentoe2b1e1f2021-10-18 08:49:03 +030088 info->fit = fit;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090089 info->node_offset = noffset;
90 info->name = algo_name;
91 info->checksum = image_get_checksum_algo(algo_name);
92 info->crypto = image_get_crypto_algo(algo_name);
93 info->padding = image_get_padding_algo(padding_name);
Simon Glasse10e2ee2021-11-12 12:28:10 -070094 info->fdt_blob = key_blob;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090095 info->required_keynode = required_keynode;
96 printf("%s:%s", algo_name, info->keyname);
97
Chia-Wei Wang1bc42222024-10-14 17:56:19 +080098 if (!info->checksum || !info->crypto) {
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090099 *err_msgp = "Unknown signature algorithm";
100 return -1;
101 }
102
103 return 0;
104}
105
106int fit_image_check_sig(const void *fit, int noffset, const void *data,
Simon Glasse10e2ee2021-11-12 12:28:10 -0700107 size_t size, const void *key_blob, int required_keynode,
108 char **err_msgp)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900109{
110 struct image_sign_info info;
111 struct image_region region;
112 uint8_t *fit_value;
113 int fit_value_len;
114
115 *err_msgp = NULL;
Simon Glasse10e2ee2021-11-12 12:28:10 -0700116 if (fit_image_setup_verify(&info, fit, noffset, key_blob,
117 required_keynode, err_msgp))
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900118 return -1;
119
120 if (fit_image_hash_get_value(fit, noffset, &fit_value,
121 &fit_value_len)) {
122 *err_msgp = "Can't get hash value property";
123 return -1;
124 }
125
126 region.data = data;
127 region.size = size;
128
129 if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
130 *err_msgp = "Verification failed";
131 return -1;
132 }
133
134 return 0;
135}
136
137static int fit_image_verify_sig(const void *fit, int image_noffset,
138 const char *data, size_t size,
Simon Glass4c5ff062021-11-12 12:28:08 -0700139 const void *key_blob, int key_offset)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900140{
141 int noffset;
142 char *err_msg = "";
143 int verified = 0;
144 int ret;
145
146 /* Process all hash subnodes of the component image node */
147 fdt_for_each_subnode(noffset, fit, image_noffset) {
148 const char *name = fit_get_name(fit, noffset, NULL);
149
Simon Glass44338902021-02-15 17:08:06 -0700150 /*
151 * We don't support this since libfdt considers names with the
152 * name root but different @ suffix to be equal
153 */
154 if (strchr(name, '@')) {
155 err_msg = "Node name contains @";
156 goto error;
157 }
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900158 if (!strncmp(name, FIT_SIG_NODENAME,
159 strlen(FIT_SIG_NODENAME))) {
Simon Glasse10e2ee2021-11-12 12:28:10 -0700160 ret = fit_image_check_sig(fit, noffset, data, size,
161 key_blob, -1, &err_msg);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900162 if (ret) {
163 puts("- ");
164 } else {
165 puts("+ ");
166 verified = 1;
167 break;
168 }
169 }
170 }
171
172 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
173 err_msg = "Corrupted or truncated tree";
174 goto error;
175 }
176
177 return verified ? 0 : -EPERM;
178
179error:
180 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
181 err_msg, fit_get_name(fit, noffset, NULL),
182 fit_get_name(fit, image_noffset, NULL));
183 return -1;
184}
185
186int fit_image_verify_required_sigs(const void *fit, int image_noffset,
187 const char *data, size_t size,
Simon Glass4c5ff062021-11-12 12:28:08 -0700188 const void *key_blob, int *no_sigsp)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900189{
190 int verify_count = 0;
191 int noffset;
Simon Glass4c5ff062021-11-12 12:28:08 -0700192 int key_node;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900193
Daniel Golle1d0d61c2025-03-29 23:23:51 +0000194#ifdef USE_HOSTCC
195 if (!key_blob)
196 return 0;
197#endif
198
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900199 /* Work out what we need to verify */
200 *no_sigsp = 1;
Simon Glass4c5ff062021-11-12 12:28:08 -0700201 key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
202 if (key_node < 0) {
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900203 debug("%s: No signature node found: %s\n", __func__,
Simon Glass4c5ff062021-11-12 12:28:08 -0700204 fdt_strerror(key_node));
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900205 return 0;
206 }
207
Simon Glass4c5ff062021-11-12 12:28:08 -0700208 fdt_for_each_subnode(noffset, key_blob, key_node) {
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900209 const char *required;
210 int ret;
211
Simon Glass4c5ff062021-11-12 12:28:08 -0700212 required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
Tom Rini7357a352020-04-07 11:58:44 -0400213 NULL);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900214 if (!required || strcmp(required, "image"))
215 continue;
216 ret = fit_image_verify_sig(fit, image_noffset, data, size,
Simon Glass4c5ff062021-11-12 12:28:08 -0700217 key_blob, noffset);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900218 if (ret) {
219 printf("Failed to verify required signature '%s'\n",
Simon Glass4c5ff062021-11-12 12:28:08 -0700220 fit_get_name(key_blob, noffset, NULL));
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900221 return ret;
222 }
223 verify_count++;
224 }
225
226 if (verify_count)
227 *no_sigsp = 0;
228
229 return 0;
230}
231
Tom Rini7357a352020-04-07 11:58:44 -0400232/**
233 * fit_config_check_sig() - Check the signature of a config
234 *
Simon Glasse3b21782021-11-12 12:28:09 -0700235 * Here we are looking at a particular signature that needs verification (here
236 * signature-1):
237 *
238 * configurations {
239 * default = "conf-1";
240 * conf-1 {
241 * kernel = "kernel-1";
242 * fdt = "fdt-1";
243 * signature-1 {
244 * algo = "sha1,rsa2048";
245 * value = <...conf 1 signature...>;
246 * };
247 * };
248 *
Tom Rini7357a352020-04-07 11:58:44 -0400249 * @fit: FIT to check
Simon Glasse3b21782021-11-12 12:28:09 -0700250 * @noffset: Offset of the signature node being checked (e.g.
251 * /configurations/conf-1/signature-1)
252 * @conf_noffset: Offset of configuration node (e.g. /configurations/conf-1)
Simon Glasse10e2ee2021-11-12 12:28:10 -0700253 * @key_blob: Blob containing the keys to check against
Simon Glasse3b21782021-11-12 12:28:09 -0700254 * @required_keynode: Offset in @key_blob of the required key node,
Tom Rini7357a352020-04-07 11:58:44 -0400255 * if any. If this is given, then the configuration wil not
256 * pass verification unless that key is used. If this is
257 * -1 then any signature will do.
Tom Rini7357a352020-04-07 11:58:44 -0400258 * @err_msgp: In the event of an error, this will be pointed to a
259 * help error string to display to the user.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100260 * Return: 0 if all verified ok, <0 on error
Tom Rini7357a352020-04-07 11:58:44 -0400261 */
Simon Glasse3b21782021-11-12 12:28:09 -0700262static int fit_config_check_sig(const void *fit, int noffset, int conf_noffset,
Simon Glasse10e2ee2021-11-12 12:28:10 -0700263 const void *key_blob, int required_keynode,
264 char **err_msgp)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900265{
John Keeping741fc332021-04-20 19:19:44 +0100266 static char * const exc_prop[] = {
Sean Anderson016f4d52022-10-20 15:41:10 -0400267 FIT_DATA_PROP,
268 FIT_DATA_SIZE_PROP,
269 FIT_DATA_POSITION_PROP,
270 FIT_DATA_OFFSET_PROP,
John Keeping741fc332021-04-20 19:19:44 +0100271 };
272
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900273 const char *prop, *end, *name;
274 struct image_sign_info info;
275 const uint32_t *strings;
Tom Rini7357a352020-04-07 11:58:44 -0400276 const char *config_name;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900277 uint8_t *fit_value;
278 int fit_value_len;
Tom Rini7357a352020-04-07 11:58:44 -0400279 bool found_config;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900280 int max_regions;
281 int i, prop_len;
282 char path[200];
283 int count;
284
Tom Rini7357a352020-04-07 11:58:44 -0400285 config_name = fit_get_name(fit, conf_noffset, NULL);
Simon Glasse10e2ee2021-11-12 12:28:10 -0700286 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, key_blob,
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900287 fit_get_name(fit, noffset, NULL),
Simon Glasse10e2ee2021-11-12 12:28:10 -0700288 fit_get_name(key_blob, required_keynode, NULL));
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900289 *err_msgp = NULL;
Simon Glasse10e2ee2021-11-12 12:28:10 -0700290 if (fit_image_setup_verify(&info, fit, noffset, key_blob,
291 required_keynode, err_msgp))
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900292 return -1;
293
294 if (fit_image_hash_get_value(fit, noffset, &fit_value,
295 &fit_value_len)) {
296 *err_msgp = "Can't get hash value property";
297 return -1;
298 }
299
300 /* Count the number of strings in the property */
301 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
302 end = prop ? prop + prop_len : prop;
303 for (name = prop, count = 0; name < end; name++)
304 if (!*name)
305 count++;
306 if (!count) {
307 *err_msgp = "Can't get hashed-nodes property";
308 return -1;
309 }
310
311 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
312 *err_msgp = "hashed-nodes property must be null-terminated";
313 return -1;
314 }
315
316 /* Add a sanity check here since we are using the stack */
317 if (count > IMAGE_MAX_HASHED_NODES) {
318 *err_msgp = "Number of hashed nodes exceeds maximum";
319 return -1;
320 }
321
322 /* Create a list of node names from those strings */
323 char *node_inc[count];
324
325 debug("Hash nodes (%d):\n", count);
Tom Rini7357a352020-04-07 11:58:44 -0400326 found_config = false;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900327 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
328 debug(" '%s'\n", name);
329 node_inc[i] = (char *)name;
Tom Rini7357a352020-04-07 11:58:44 -0400330 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
331 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
332 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
333 debug(" (found config node %s)", config_name);
334 found_config = true;
335 }
336 }
337 if (!found_config) {
338 *err_msgp = "Selected config not in hashed nodes";
339 return -1;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900340 }
341
342 /*
343 * Each node can generate one region for each sub-node. Allow for
344 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
345 */
346 max_regions = 20 + count * 7;
347 struct fdt_region fdt_regions[max_regions];
348
349 /* Get a list of regions to hash */
350 count = fdt_find_regions(fit, node_inc, count,
351 exc_prop, ARRAY_SIZE(exc_prop),
352 fdt_regions, max_regions - 1,
353 path, sizeof(path), 0);
354 if (count < 0) {
355 *err_msgp = "Failed to hash configuration";
356 return -1;
357 }
358 if (count == 0) {
359 *err_msgp = "No data to hash";
360 return -1;
361 }
362 if (count >= max_regions - 1) {
363 *err_msgp = "Too many hash regions";
364 return -1;
365 }
366
367 /* Add the strings */
368 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
369 if (strings) {
370 /*
371 * The strings region offset must be a static 0x0.
372 * This is set in tool/image-host.c
373 */
374 fdt_regions[count].offset = fdt_off_dt_strings(fit);
375 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
376 count++;
377 }
378
379 /* Allocate the region list on the stack */
380 struct image_region region[count];
381
382 fit_region_make_list(fit, fdt_regions, count, region);
383 if (info.crypto->verify(&info, region, count, fit_value,
384 fit_value_len)) {
385 *err_msgp = "Verification failed";
386 return -1;
387 }
388
389 return 0;
390}
391
Simon Glass4c5ff062021-11-12 12:28:08 -0700392/**
393 * fit_config_verify_key() - Verify that a configuration is signed with a key
394 *
395 * Here we are looking at a particular configuration that needs verification:
396 *
397 * configurations {
398 * default = "conf-1";
399 * conf-1 {
400 * kernel = "kernel-1";
401 * fdt = "fdt-1";
402 * signature-1 {
403 * algo = "sha1,rsa2048";
404 * value = <...conf 1 signature...>;
405 * };
406 * };
407 *
408 * We must check each of the signature subnodes of conf-1. Hopefully one of them
409 * will match the key at key_offset.
410 *
411 * @fit: FIT to check
412 * @conf_noffset: Offset of the configuration node to check (e.g.
413 * /configurations/conf-1)
414 * @key_blob: Blob containing the keys to check against
415 * @key_offset: Offset of the key to check within @key_blob
416 * @return 0 if OK, -EPERM if any signatures did not verify, or the
417 * configuration node has an invalid name
418 */
419static int fit_config_verify_key(const void *fit, int conf_noffset,
420 const void *key_blob, int key_offset)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900421{
422 int noffset;
Alexandru Gagniuc72fdd3a2021-01-11 08:46:58 -0600423 char *err_msg = "No 'signature' subnode found";
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900424 int verified = 0;
425 int ret;
426
427 /* Process all hash subnodes of the component conf node */
428 fdt_for_each_subnode(noffset, fit, conf_noffset) {
429 const char *name = fit_get_name(fit, noffset, NULL);
430
431 if (!strncmp(name, FIT_SIG_NODENAME,
432 strlen(FIT_SIG_NODENAME))) {
Simon Glasse3b21782021-11-12 12:28:09 -0700433 ret = fit_config_check_sig(fit, noffset, conf_noffset,
Simon Glasse10e2ee2021-11-12 12:28:10 -0700434 key_blob, key_offset,
435 &err_msg);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900436 if (ret) {
437 puts("- ");
438 } else {
439 puts("+ ");
440 verified = 1;
441 break;
442 }
443 }
444 }
445
446 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
447 err_msg = "Corrupted or truncated tree";
448 goto error;
449 }
450
Tom Rini7357a352020-04-07 11:58:44 -0400451 if (verified)
452 return 0;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900453
454error:
455 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
456 err_msg, fit_get_name(fit, noffset, NULL),
457 fit_get_name(fit, conf_noffset, NULL));
Tom Rini7357a352020-04-07 11:58:44 -0400458 return -EPERM;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900459}
460
Simon Glass4c5ff062021-11-12 12:28:08 -0700461/**
462 * fit_config_verify_required_keys() - verify any required signatures for config
463 *
464 * This looks through all the signatures we expect and verifies that at least
465 * all the required ones are valid signatures for the configuration
466 *
467 * @fit: FIT to check
468 * @conf_noffset: Offset of the configuration node to check (e.g.
469 * /configurations/conf-1)
470 * @key_blob: Blob containing the keys to check against
471 * @return 0 if OK, -EPERM if any signatures did not verify, or the
472 * configuration node has an invalid name
473 */
474static int fit_config_verify_required_keys(const void *fit, int conf_noffset,
475 const void *key_blob)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900476{
Simon Glass44338902021-02-15 17:08:06 -0700477 const char *name = fit_get_name(fit, conf_noffset, NULL);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900478 int noffset;
Simon Glass4c5ff062021-11-12 12:28:08 -0700479 int key_node;
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700480 int verified = 0;
481 int reqd_sigs = 0;
482 bool reqd_policy_all = true;
483 const char *reqd_mode;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900484
Daniel Golle1d0d61c2025-03-29 23:23:51 +0000485#ifdef USE_HOSTCC
486 if (!key_blob)
487 return 0;
488#endif
489
Simon Glass44338902021-02-15 17:08:06 -0700490 /*
491 * We don't support this since libfdt considers names with the
492 * name root but different @ suffix to be equal
493 */
494 if (strchr(name, '@')) {
495 printf("Configuration node '%s' contains '@'\n", name);
496 return -EPERM;
497 }
498
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900499 /* Work out what we need to verify */
Simon Glass4c5ff062021-11-12 12:28:08 -0700500 key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
501 if (key_node < 0) {
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900502 debug("%s: No signature node found: %s\n", __func__,
Simon Glass4c5ff062021-11-12 12:28:08 -0700503 fdt_strerror(key_node));
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900504 return 0;
505 }
506
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700507 /* Get required-mode policy property from DTB */
Simon Glass4c5ff062021-11-12 12:28:08 -0700508 reqd_mode = fdt_getprop(key_blob, key_node, "required-mode", NULL);
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700509 if (reqd_mode && !strcmp(reqd_mode, "any"))
510 reqd_policy_all = false;
511
512 debug("%s: required-mode policy set to '%s'\n", __func__,
513 reqd_policy_all ? "all" : "any");
514
Simon Glass4c5ff062021-11-12 12:28:08 -0700515 /*
516 * The algorithm here is a little convoluted due to how we want it to
517 * work. Here we work through each of the signature nodes in the
518 * public-key area. These are in the U-Boot control devicetree. Each
519 * node was created by signing a configuration, so we check if it is
520 * 'required' and if so, request that it be verified.
521 */
522 fdt_for_each_subnode(noffset, key_blob, key_node) {
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900523 const char *required;
524 int ret;
525
Simon Glass4c5ff062021-11-12 12:28:08 -0700526 required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
Tom Rini7357a352020-04-07 11:58:44 -0400527 NULL);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900528 if (!required || strcmp(required, "conf"))
529 continue;
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700530
531 reqd_sigs++;
532
Simon Glass4c5ff062021-11-12 12:28:08 -0700533 ret = fit_config_verify_key(fit, conf_noffset, key_blob,
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900534 noffset);
535 if (ret) {
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700536 if (reqd_policy_all) {
537 printf("Failed to verify required signature '%s'\n",
Simon Glass4c5ff062021-11-12 12:28:08 -0700538 fit_get_name(key_blob, noffset, NULL));
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700539 return ret;
540 }
541 } else {
542 verified++;
543 if (!reqd_policy_all)
544 break;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900545 }
546 }
547
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700548 if (reqd_sigs && !verified) {
549 printf("Failed to verify 'any' of the required signature(s)\n");
550 return -EPERM;
551 }
552
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900553 return 0;
554}
555
556int fit_config_verify(const void *fit, int conf_noffset)
557{
Simon Glass4c5ff062021-11-12 12:28:08 -0700558 return fit_config_verify_required_keys(fit, conf_noffset,
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900559 gd_fdt_blob());
560}