blob: 897e04c7a38b750e23ab8ea85b2b560e5e3e8cd2 [file] [log] [blame]
AKASHI Takahirob983cc22020-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 Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
AKASHI Takahirob983cc22020-02-21 15:12:55 +090012#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
AKASHI Takahirob983cc22020-02-21 15:12:55 +090014DECLARE_GLOBAL_DATA_PTR;
15#endif /* !USE_HOSTCC*/
Masahiro Yamada64045a62020-04-16 18:30:18 +090016#include <fdt_region.h>
AKASHI Takahirob983cc22020-02-21 15:12:55 +090017#include <image.h>
18#include <u-boot/rsa.h>
19#include <u-boot/rsa-checksum.h>
20
21#define IMAGE_MAX_HASHED_NODES 100
22
AKASHI Takahirob983cc22020-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
75 if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
76 *err_msgp = "Total size too large";
77 return 1;
78 }
79
80 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
81 *err_msgp = "Can't get hash algo property";
82 return -1;
83 }
84
85 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
86 if (!padding_name)
87 padding_name = RSA_DEFAULT_PADDING_NAME;
88
89 memset(info, '\0', sizeof(*info));
Tom Rini1f47e2a2020-04-07 11:58:44 -040090 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
AKASHI Takahirob983cc22020-02-21 15:12:55 +090091 info->fit = (void *)fit;
92 info->node_offset = noffset;
93 info->name = algo_name;
94 info->checksum = image_get_checksum_algo(algo_name);
95 info->crypto = image_get_crypto_algo(algo_name);
96 info->padding = image_get_padding_algo(padding_name);
97 info->fdt_blob = gd_fdt_blob();
98 info->required_keynode = required_keynode;
99 printf("%s:%s", algo_name, info->keyname);
100
101 if (!info->checksum || !info->crypto || !info->padding) {
102 *err_msgp = "Unknown signature algorithm";
103 return -1;
104 }
105
106 return 0;
107}
108
109int fit_image_check_sig(const void *fit, int noffset, const void *data,
110 size_t size, int required_keynode, char **err_msgp)
111{
112 struct image_sign_info info;
113 struct image_region region;
114 uint8_t *fit_value;
115 int fit_value_len;
116
117 *err_msgp = NULL;
118 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
119 err_msgp))
120 return -1;
121
122 if (fit_image_hash_get_value(fit, noffset, &fit_value,
123 &fit_value_len)) {
124 *err_msgp = "Can't get hash value property";
125 return -1;
126 }
127
128 region.data = data;
129 region.size = size;
130
131 if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
132 *err_msgp = "Verification failed";
133 return -1;
134 }
135
136 return 0;
137}
138
139static int fit_image_verify_sig(const void *fit, int image_noffset,
140 const char *data, size_t size,
141 const void *sig_blob, int sig_offset)
142{
143 int noffset;
144 char *err_msg = "";
145 int verified = 0;
146 int ret;
147
148 /* Process all hash subnodes of the component image node */
149 fdt_for_each_subnode(noffset, fit, image_noffset) {
150 const char *name = fit_get_name(fit, noffset, NULL);
151
152 if (!strncmp(name, FIT_SIG_NODENAME,
153 strlen(FIT_SIG_NODENAME))) {
154 ret = fit_image_check_sig(fit, noffset, data,
155 size, -1, &err_msg);
156 if (ret) {
157 puts("- ");
158 } else {
159 puts("+ ");
160 verified = 1;
161 break;
162 }
163 }
164 }
165
166 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
167 err_msg = "Corrupted or truncated tree";
168 goto error;
169 }
170
171 return verified ? 0 : -EPERM;
172
173error:
174 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
175 err_msg, fit_get_name(fit, noffset, NULL),
176 fit_get_name(fit, image_noffset, NULL));
177 return -1;
178}
179
180int fit_image_verify_required_sigs(const void *fit, int image_noffset,
181 const char *data, size_t size,
182 const void *sig_blob, int *no_sigsp)
183{
184 int verify_count = 0;
185 int noffset;
186 int sig_node;
187
188 /* Work out what we need to verify */
189 *no_sigsp = 1;
190 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
191 if (sig_node < 0) {
192 debug("%s: No signature node found: %s\n", __func__,
193 fdt_strerror(sig_node));
194 return 0;
195 }
196
197 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
198 const char *required;
199 int ret;
200
Tom Rini1f47e2a2020-04-07 11:58:44 -0400201 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
202 NULL);
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900203 if (!required || strcmp(required, "image"))
204 continue;
205 ret = fit_image_verify_sig(fit, image_noffset, data, size,
206 sig_blob, noffset);
207 if (ret) {
208 printf("Failed to verify required signature '%s'\n",
209 fit_get_name(sig_blob, noffset, NULL));
210 return ret;
211 }
212 verify_count++;
213 }
214
215 if (verify_count)
216 *no_sigsp = 0;
217
218 return 0;
219}
220
Tom Rini1f47e2a2020-04-07 11:58:44 -0400221/**
222 * fit_config_check_sig() - Check the signature of a config
223 *
224 * @fit: FIT to check
225 * @noffset: Offset of configuration node (e.g. /configurations/conf-1)
226 * @required_keynode: Offset in the control FDT of the required key node,
227 * if any. If this is given, then the configuration wil not
228 * pass verification unless that key is used. If this is
229 * -1 then any signature will do.
230 * @conf_noffset: Offset of the configuration subnode being checked (e.g.
231 * /configurations/conf-1/kernel)
232 * @err_msgp: In the event of an error, this will be pointed to a
233 * help error string to display to the user.
234 * @return 0 if all verified ok, <0 on error
235 */
236static int fit_config_check_sig(const void *fit, int noffset,
237 int required_keynode, int conf_noffset,
238 char **err_msgp)
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900239{
Philippe Reynesc5229492020-04-29 15:26:17 +0200240 char * const exc_prop[] = {"data", "data-size", "data-position"};
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900241 const char *prop, *end, *name;
242 struct image_sign_info info;
243 const uint32_t *strings;
Tom Rini1f47e2a2020-04-07 11:58:44 -0400244 const char *config_name;
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900245 uint8_t *fit_value;
246 int fit_value_len;
Tom Rini1f47e2a2020-04-07 11:58:44 -0400247 bool found_config;
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900248 int max_regions;
249 int i, prop_len;
250 char path[200];
251 int count;
252
Tom Rini1f47e2a2020-04-07 11:58:44 -0400253 config_name = fit_get_name(fit, conf_noffset, NULL);
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900254 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
255 fit_get_name(fit, noffset, NULL),
256 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
257 *err_msgp = NULL;
258 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
259 err_msgp))
260 return -1;
261
262 if (fit_image_hash_get_value(fit, noffset, &fit_value,
263 &fit_value_len)) {
264 *err_msgp = "Can't get hash value property";
265 return -1;
266 }
267
268 /* Count the number of strings in the property */
269 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
270 end = prop ? prop + prop_len : prop;
271 for (name = prop, count = 0; name < end; name++)
272 if (!*name)
273 count++;
274 if (!count) {
275 *err_msgp = "Can't get hashed-nodes property";
276 return -1;
277 }
278
279 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
280 *err_msgp = "hashed-nodes property must be null-terminated";
281 return -1;
282 }
283
284 /* Add a sanity check here since we are using the stack */
285 if (count > IMAGE_MAX_HASHED_NODES) {
286 *err_msgp = "Number of hashed nodes exceeds maximum";
287 return -1;
288 }
289
290 /* Create a list of node names from those strings */
291 char *node_inc[count];
292
293 debug("Hash nodes (%d):\n", count);
Tom Rini1f47e2a2020-04-07 11:58:44 -0400294 found_config = false;
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900295 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
296 debug(" '%s'\n", name);
297 node_inc[i] = (char *)name;
Tom Rini1f47e2a2020-04-07 11:58:44 -0400298 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
299 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
300 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
301 debug(" (found config node %s)", config_name);
302 found_config = true;
303 }
304 }
305 if (!found_config) {
306 *err_msgp = "Selected config not in hashed nodes";
307 return -1;
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900308 }
309
310 /*
311 * Each node can generate one region for each sub-node. Allow for
312 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
313 */
314 max_regions = 20 + count * 7;
315 struct fdt_region fdt_regions[max_regions];
316
317 /* Get a list of regions to hash */
318 count = fdt_find_regions(fit, node_inc, count,
319 exc_prop, ARRAY_SIZE(exc_prop),
320 fdt_regions, max_regions - 1,
321 path, sizeof(path), 0);
322 if (count < 0) {
323 *err_msgp = "Failed to hash configuration";
324 return -1;
325 }
326 if (count == 0) {
327 *err_msgp = "No data to hash";
328 return -1;
329 }
330 if (count >= max_regions - 1) {
331 *err_msgp = "Too many hash regions";
332 return -1;
333 }
334
335 /* Add the strings */
336 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
337 if (strings) {
338 /*
339 * The strings region offset must be a static 0x0.
340 * This is set in tool/image-host.c
341 */
342 fdt_regions[count].offset = fdt_off_dt_strings(fit);
343 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
344 count++;
345 }
346
347 /* Allocate the region list on the stack */
348 struct image_region region[count];
349
350 fit_region_make_list(fit, fdt_regions, count, region);
351 if (info.crypto->verify(&info, region, count, fit_value,
352 fit_value_len)) {
353 *err_msgp = "Verification failed";
354 return -1;
355 }
356
357 return 0;
358}
359
360static int fit_config_verify_sig(const void *fit, int conf_noffset,
361 const void *sig_blob, int sig_offset)
362{
363 int noffset;
Alexandru Gagniuc76c78a52021-01-11 08:46:58 -0600364 char *err_msg = "No 'signature' subnode found";
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900365 int verified = 0;
366 int ret;
367
368 /* Process all hash subnodes of the component conf node */
369 fdt_for_each_subnode(noffset, fit, conf_noffset) {
370 const char *name = fit_get_name(fit, noffset, NULL);
371
372 if (!strncmp(name, FIT_SIG_NODENAME,
373 strlen(FIT_SIG_NODENAME))) {
374 ret = fit_config_check_sig(fit, noffset, sig_offset,
Tom Rini1f47e2a2020-04-07 11:58:44 -0400375 conf_noffset, &err_msg);
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900376 if (ret) {
377 puts("- ");
378 } else {
379 puts("+ ");
380 verified = 1;
381 break;
382 }
383 }
384 }
385
386 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
387 err_msg = "Corrupted or truncated tree";
388 goto error;
389 }
390
Tom Rini1f47e2a2020-04-07 11:58:44 -0400391 if (verified)
392 return 0;
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900393
394error:
395 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
396 err_msg, fit_get_name(fit, noffset, NULL),
397 fit_get_name(fit, conf_noffset, NULL));
Tom Rini1f47e2a2020-04-07 11:58:44 -0400398 return -EPERM;
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900399}
400
401int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
402 const void *sig_blob)
403{
404 int noffset;
405 int sig_node;
Thirupathaiah Annapureddy182eeef2020-08-16 23:01:09 -0700406 int verified = 0;
407 int reqd_sigs = 0;
408 bool reqd_policy_all = true;
409 const char *reqd_mode;
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900410
411 /* Work out what we need to verify */
412 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
413 if (sig_node < 0) {
414 debug("%s: No signature node found: %s\n", __func__,
415 fdt_strerror(sig_node));
416 return 0;
417 }
418
Thirupathaiah Annapureddy182eeef2020-08-16 23:01:09 -0700419 /* Get required-mode policy property from DTB */
420 reqd_mode = fdt_getprop(sig_blob, sig_node, "required-mode", NULL);
421 if (reqd_mode && !strcmp(reqd_mode, "any"))
422 reqd_policy_all = false;
423
424 debug("%s: required-mode policy set to '%s'\n", __func__,
425 reqd_policy_all ? "all" : "any");
426
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900427 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
428 const char *required;
429 int ret;
430
Tom Rini1f47e2a2020-04-07 11:58:44 -0400431 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
432 NULL);
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900433 if (!required || strcmp(required, "conf"))
434 continue;
Thirupathaiah Annapureddy182eeef2020-08-16 23:01:09 -0700435
436 reqd_sigs++;
437
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900438 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
439 noffset);
440 if (ret) {
Thirupathaiah Annapureddy182eeef2020-08-16 23:01:09 -0700441 if (reqd_policy_all) {
442 printf("Failed to verify required signature '%s'\n",
443 fit_get_name(sig_blob, noffset, NULL));
444 return ret;
445 }
446 } else {
447 verified++;
448 if (!reqd_policy_all)
449 break;
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900450 }
451 }
452
Thirupathaiah Annapureddy182eeef2020-08-16 23:01:09 -0700453 if (reqd_sigs && !verified) {
454 printf("Failed to verify 'any' of the required signature(s)\n");
455 return -EPERM;
456 }
457
AKASHI Takahirob983cc22020-02-21 15:12:55 +0900458 return 0;
459}
460
461int fit_config_verify(const void *fit, int conf_noffset)
462{
463 return fit_config_verify_required_sigs(fit, conf_noffset,
464 gd_fdt_blob());
465}