blob: e95e64bd2fe92da8b14e2e84445dec837ff2979d [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
10#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090012#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090014DECLARE_GLOBAL_DATA_PTR;
15#endif /* !USE_HOSTCC*/
Masahiro Yamada6dd10522020-04-16 18:30:18 +090016#include <fdt_region.h>
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090017#include <image.h>
18#include <u-boot/rsa.h>
Alexandru Gagniucdb182c42021-02-19 12:45:10 -060019#include <u-boot/hash-checksum.h>
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090020
21#define IMAGE_MAX_HASHED_NODES 100
22
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090023/**
24 * fit_region_make_list() - Make a list of image regions
25 *
26 * Given a list of fdt_regions, create a list of image_regions. This is a
27 * simple conversion routine since the FDT and image code use different
28 * structures.
29 *
30 * @fit: FIT image
31 * @fdt_regions: Pointer to FDT regions
32 * @count: Number of FDT regions
33 * @region: Pointer to image regions, which must hold @count records. If
34 * region is NULL, then (except for an SPL build) the array will be
35 * allocated.
36 * @return: Pointer to image regions
37 */
38struct image_region *fit_region_make_list(const void *fit,
39 struct fdt_region *fdt_regions,
40 int count,
41 struct image_region *region)
42{
43 int i;
44
45 debug("Hash regions:\n");
46 debug("%10s %10s\n", "Offset", "Size");
47
48 /*
49 * Use malloc() except in SPL (to save code size). In SPL the caller
50 * must allocate the array.
51 */
52#ifndef CONFIG_SPL_BUILD
53 if (!region)
54 region = calloc(sizeof(*region), count);
55#endif
56 if (!region)
57 return NULL;
58 for (i = 0; i < count; i++) {
59 debug("%10x %10x\n", fdt_regions[i].offset,
60 fdt_regions[i].size);
61 region[i].data = fit + fdt_regions[i].offset;
62 region[i].size = fdt_regions[i].size;
63 }
64
65 return region;
66}
67
68static int fit_image_setup_verify(struct image_sign_info *info,
69 const void *fit, int noffset,
70 int required_keynode, char **err_msgp)
71{
72 char *algo_name;
73 const char *padding_name;
74
Simon Glassf16dc6c2021-09-25 19:43:16 -060075#ifndef USE_HOSTCC
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090076 if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
77 *err_msgp = "Total size too large";
78 return 1;
79 }
Simon Glassf16dc6c2021-09-25 19:43:16 -060080#endif
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090081 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
82 *err_msgp = "Can't get hash algo property";
83 return -1;
84 }
85
86 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
87 if (!padding_name)
88 padding_name = RSA_DEFAULT_PADDING_NAME;
89
90 memset(info, '\0', sizeof(*info));
Tom Rini7357a352020-04-07 11:58:44 -040091 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +090092 info->fit = (void *)fit;
93 info->node_offset = noffset;
94 info->name = algo_name;
95 info->checksum = image_get_checksum_algo(algo_name);
96 info->crypto = image_get_crypto_algo(algo_name);
97 info->padding = image_get_padding_algo(padding_name);
98 info->fdt_blob = gd_fdt_blob();
99 info->required_keynode = required_keynode;
100 printf("%s:%s", algo_name, info->keyname);
101
102 if (!info->checksum || !info->crypto || !info->padding) {
103 *err_msgp = "Unknown signature algorithm";
104 return -1;
105 }
106
107 return 0;
108}
109
110int fit_image_check_sig(const void *fit, int noffset, const void *data,
111 size_t size, int required_keynode, char **err_msgp)
112{
113 struct image_sign_info info;
114 struct image_region region;
115 uint8_t *fit_value;
116 int fit_value_len;
117
118 *err_msgp = NULL;
119 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
120 err_msgp))
121 return -1;
122
123 if (fit_image_hash_get_value(fit, noffset, &fit_value,
124 &fit_value_len)) {
125 *err_msgp = "Can't get hash value property";
126 return -1;
127 }
128
129 region.data = data;
130 region.size = size;
131
132 if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
133 *err_msgp = "Verification failed";
134 return -1;
135 }
136
137 return 0;
138}
139
140static int fit_image_verify_sig(const void *fit, int image_noffset,
141 const char *data, size_t size,
142 const void *sig_blob, int sig_offset)
143{
144 int noffset;
145 char *err_msg = "";
146 int verified = 0;
147 int ret;
148
149 /* Process all hash subnodes of the component image node */
150 fdt_for_each_subnode(noffset, fit, image_noffset) {
151 const char *name = fit_get_name(fit, noffset, NULL);
152
Simon Glass44338902021-02-15 17:08:06 -0700153 /*
154 * We don't support this since libfdt considers names with the
155 * name root but different @ suffix to be equal
156 */
157 if (strchr(name, '@')) {
158 err_msg = "Node name contains @";
159 goto error;
160 }
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900161 if (!strncmp(name, FIT_SIG_NODENAME,
162 strlen(FIT_SIG_NODENAME))) {
163 ret = fit_image_check_sig(fit, noffset, data,
164 size, -1, &err_msg);
165 if (ret) {
166 puts("- ");
167 } else {
168 puts("+ ");
169 verified = 1;
170 break;
171 }
172 }
173 }
174
175 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
176 err_msg = "Corrupted or truncated tree";
177 goto error;
178 }
179
180 return verified ? 0 : -EPERM;
181
182error:
183 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
184 err_msg, fit_get_name(fit, noffset, NULL),
185 fit_get_name(fit, image_noffset, NULL));
186 return -1;
187}
188
189int fit_image_verify_required_sigs(const void *fit, int image_noffset,
190 const char *data, size_t size,
191 const void *sig_blob, int *no_sigsp)
192{
193 int verify_count = 0;
194 int noffset;
195 int sig_node;
196
197 /* Work out what we need to verify */
198 *no_sigsp = 1;
199 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
200 if (sig_node < 0) {
201 debug("%s: No signature node found: %s\n", __func__,
202 fdt_strerror(sig_node));
203 return 0;
204 }
205
206 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
207 const char *required;
208 int ret;
209
Tom Rini7357a352020-04-07 11:58:44 -0400210 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
211 NULL);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900212 if (!required || strcmp(required, "image"))
213 continue;
214 ret = fit_image_verify_sig(fit, image_noffset, data, size,
215 sig_blob, noffset);
216 if (ret) {
217 printf("Failed to verify required signature '%s'\n",
218 fit_get_name(sig_blob, noffset, NULL));
219 return ret;
220 }
221 verify_count++;
222 }
223
224 if (verify_count)
225 *no_sigsp = 0;
226
227 return 0;
228}
229
Tom Rini7357a352020-04-07 11:58:44 -0400230/**
231 * fit_config_check_sig() - Check the signature of a config
232 *
233 * @fit: FIT to check
234 * @noffset: Offset of configuration node (e.g. /configurations/conf-1)
235 * @required_keynode: Offset in the control FDT of the required key node,
236 * if any. If this is given, then the configuration wil not
237 * pass verification unless that key is used. If this is
238 * -1 then any signature will do.
239 * @conf_noffset: Offset of the configuration subnode being checked (e.g.
240 * /configurations/conf-1/kernel)
241 * @err_msgp: In the event of an error, this will be pointed to a
242 * help error string to display to the user.
243 * @return 0 if all verified ok, <0 on error
244 */
245static int fit_config_check_sig(const void *fit, int noffset,
246 int required_keynode, int conf_noffset,
247 char **err_msgp)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900248{
John Keeping741fc332021-04-20 19:19:44 +0100249 static char * const exc_prop[] = {
250 "data",
251 "data-size",
252 "data-position",
253 "data-offset"
254 };
255
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900256 const char *prop, *end, *name;
257 struct image_sign_info info;
258 const uint32_t *strings;
Tom Rini7357a352020-04-07 11:58:44 -0400259 const char *config_name;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900260 uint8_t *fit_value;
261 int fit_value_len;
Tom Rini7357a352020-04-07 11:58:44 -0400262 bool found_config;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900263 int max_regions;
264 int i, prop_len;
265 char path[200];
266 int count;
267
Tom Rini7357a352020-04-07 11:58:44 -0400268 config_name = fit_get_name(fit, conf_noffset, NULL);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900269 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
270 fit_get_name(fit, noffset, NULL),
271 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
272 *err_msgp = NULL;
273 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
274 err_msgp))
275 return -1;
276
277 if (fit_image_hash_get_value(fit, noffset, &fit_value,
278 &fit_value_len)) {
279 *err_msgp = "Can't get hash value property";
280 return -1;
281 }
282
283 /* Count the number of strings in the property */
284 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
285 end = prop ? prop + prop_len : prop;
286 for (name = prop, count = 0; name < end; name++)
287 if (!*name)
288 count++;
289 if (!count) {
290 *err_msgp = "Can't get hashed-nodes property";
291 return -1;
292 }
293
294 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
295 *err_msgp = "hashed-nodes property must be null-terminated";
296 return -1;
297 }
298
299 /* Add a sanity check here since we are using the stack */
300 if (count > IMAGE_MAX_HASHED_NODES) {
301 *err_msgp = "Number of hashed nodes exceeds maximum";
302 return -1;
303 }
304
305 /* Create a list of node names from those strings */
306 char *node_inc[count];
307
308 debug("Hash nodes (%d):\n", count);
Tom Rini7357a352020-04-07 11:58:44 -0400309 found_config = false;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900310 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
311 debug(" '%s'\n", name);
312 node_inc[i] = (char *)name;
Tom Rini7357a352020-04-07 11:58:44 -0400313 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
314 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
315 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
316 debug(" (found config node %s)", config_name);
317 found_config = true;
318 }
319 }
320 if (!found_config) {
321 *err_msgp = "Selected config not in hashed nodes";
322 return -1;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900323 }
324
325 /*
326 * Each node can generate one region for each sub-node. Allow for
327 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
328 */
329 max_regions = 20 + count * 7;
330 struct fdt_region fdt_regions[max_regions];
331
332 /* Get a list of regions to hash */
333 count = fdt_find_regions(fit, node_inc, count,
334 exc_prop, ARRAY_SIZE(exc_prop),
335 fdt_regions, max_regions - 1,
336 path, sizeof(path), 0);
337 if (count < 0) {
338 *err_msgp = "Failed to hash configuration";
339 return -1;
340 }
341 if (count == 0) {
342 *err_msgp = "No data to hash";
343 return -1;
344 }
345 if (count >= max_regions - 1) {
346 *err_msgp = "Too many hash regions";
347 return -1;
348 }
349
350 /* Add the strings */
351 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
352 if (strings) {
353 /*
354 * The strings region offset must be a static 0x0.
355 * This is set in tool/image-host.c
356 */
357 fdt_regions[count].offset = fdt_off_dt_strings(fit);
358 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
359 count++;
360 }
361
362 /* Allocate the region list on the stack */
363 struct image_region region[count];
364
365 fit_region_make_list(fit, fdt_regions, count, region);
366 if (info.crypto->verify(&info, region, count, fit_value,
367 fit_value_len)) {
368 *err_msgp = "Verification failed";
369 return -1;
370 }
371
372 return 0;
373}
374
375static int fit_config_verify_sig(const void *fit, int conf_noffset,
376 const void *sig_blob, int sig_offset)
377{
378 int noffset;
Alexandru Gagniuc72fdd3a2021-01-11 08:46:58 -0600379 char *err_msg = "No 'signature' subnode found";
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900380 int verified = 0;
381 int ret;
382
383 /* Process all hash subnodes of the component conf node */
384 fdt_for_each_subnode(noffset, fit, conf_noffset) {
385 const char *name = fit_get_name(fit, noffset, NULL);
386
387 if (!strncmp(name, FIT_SIG_NODENAME,
388 strlen(FIT_SIG_NODENAME))) {
389 ret = fit_config_check_sig(fit, noffset, sig_offset,
Tom Rini7357a352020-04-07 11:58:44 -0400390 conf_noffset, &err_msg);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900391 if (ret) {
392 puts("- ");
393 } else {
394 puts("+ ");
395 verified = 1;
396 break;
397 }
398 }
399 }
400
401 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
402 err_msg = "Corrupted or truncated tree";
403 goto error;
404 }
405
Tom Rini7357a352020-04-07 11:58:44 -0400406 if (verified)
407 return 0;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900408
409error:
410 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
411 err_msg, fit_get_name(fit, noffset, NULL),
412 fit_get_name(fit, conf_noffset, NULL));
Tom Rini7357a352020-04-07 11:58:44 -0400413 return -EPERM;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900414}
415
Simon Glass44338902021-02-15 17:08:06 -0700416static int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
417 const void *sig_blob)
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900418{
Simon Glass44338902021-02-15 17:08:06 -0700419 const char *name = fit_get_name(fit, conf_noffset, NULL);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900420 int noffset;
421 int sig_node;
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700422 int verified = 0;
423 int reqd_sigs = 0;
424 bool reqd_policy_all = true;
425 const char *reqd_mode;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900426
Simon Glass44338902021-02-15 17:08:06 -0700427 /*
428 * We don't support this since libfdt considers names with the
429 * name root but different @ suffix to be equal
430 */
431 if (strchr(name, '@')) {
432 printf("Configuration node '%s' contains '@'\n", name);
433 return -EPERM;
434 }
435
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900436 /* Work out what we need to verify */
437 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
438 if (sig_node < 0) {
439 debug("%s: No signature node found: %s\n", __func__,
440 fdt_strerror(sig_node));
441 return 0;
442 }
443
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700444 /* Get required-mode policy property from DTB */
445 reqd_mode = fdt_getprop(sig_blob, sig_node, "required-mode", NULL);
446 if (reqd_mode && !strcmp(reqd_mode, "any"))
447 reqd_policy_all = false;
448
449 debug("%s: required-mode policy set to '%s'\n", __func__,
450 reqd_policy_all ? "all" : "any");
451
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900452 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
453 const char *required;
454 int ret;
455
Tom Rini7357a352020-04-07 11:58:44 -0400456 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
457 NULL);
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900458 if (!required || strcmp(required, "conf"))
459 continue;
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700460
461 reqd_sigs++;
462
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900463 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
464 noffset);
465 if (ret) {
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700466 if (reqd_policy_all) {
467 printf("Failed to verify required signature '%s'\n",
468 fit_get_name(sig_blob, noffset, NULL));
469 return ret;
470 }
471 } else {
472 verified++;
473 if (!reqd_policy_all)
474 break;
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900475 }
476 }
477
Thirupathaiah Annapureddy77564b02020-08-16 23:01:09 -0700478 if (reqd_sigs && !verified) {
479 printf("Failed to verify 'any' of the required signature(s)\n");
480 return -EPERM;
481 }
482
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +0900483 return 0;
484}
485
486int fit_config_verify(const void *fit, int conf_noffset)
487{
488 return fit_config_verify_required_sigs(fit, conf_noffset,
489 gd_fdt_blob());
490}