blob: 8e43671714b8936f572c47bd2971d492c7cbad71 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass10a1eca2013-05-07 06:11:54 +00002/*
3 * Copyright (c) 2013, Google Inc.
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass10a1eca2013-05-07 06:11:54 +00009 */
10
11#include "mkimage.h"
Simon Glassa51991d2014-06-12 07:24:53 -060012#include <bootm.h>
Simon Glass10a1eca2013-05-07 06:11:54 +000013#include <image.h>
Simon Glassfbabc0f2013-06-13 15:10:01 -070014#include <version.h>
Simon Glass10a1eca2013-05-07 06:11:54 +000015
16/**
Simon Glass0ce6e3d2013-05-07 06:11:56 +000017 * fit_set_hash_value - set hash value in requested has node
18 * @fit: pointer to the FIT format image header
19 * @noffset: hash node offset
20 * @value: hash value to be set
21 * @value_len: hash value length
22 *
23 * fit_set_hash_value() attempts to set hash value in a node at offset
24 * given and returns operation status to the caller.
25 *
26 * returns
27 * 0, on success
28 * -1, on failure
29 */
30static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
31 int value_len)
32{
33 int ret;
34
35 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
36 if (ret) {
37 printf("Can't set hash '%s' property for '%s' node(%s)\n",
38 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
39 fdt_strerror(ret));
Simon Glassec539f72016-07-03 09:40:44 -060040 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
Simon Glass0ce6e3d2013-05-07 06:11:56 +000041 }
42
43 return 0;
44}
45
46/**
Simon Glass25d6e6b2013-05-07 06:11:55 +000047 * fit_image_process_hash - Process a single subnode of the images/ node
48 *
49 * Check each subnode and process accordingly. For hash nodes we generate
50 * a hash of the supplised data and store it in the node.
51 *
52 * @fit: pointer to the FIT format image header
53 * @image_name: name of image being processes (used to display errors)
54 * @noffset: subnode offset
55 * @data: data to process
56 * @size: size of data in bytes
57 * @return 0 if ok, -1 on error
58 */
59static int fit_image_process_hash(void *fit, const char *image_name,
60 int noffset, const void *data, size_t size)
61{
62 uint8_t value[FIT_MAX_HASH_LEN];
Simon Glassee382652013-05-07 06:12:01 +000063 const char *node_name;
Simon Glass25d6e6b2013-05-07 06:11:55 +000064 int value_len;
65 char *algo;
Simon Glassec539f72016-07-03 09:40:44 -060066 int ret;
Simon Glass25d6e6b2013-05-07 06:11:55 +000067
Simon Glassee382652013-05-07 06:12:01 +000068 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass25d6e6b2013-05-07 06:11:55 +000069
70 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
71 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000072 node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060073 return -ENOENT;
Simon Glass25d6e6b2013-05-07 06:11:55 +000074 }
75
76 if (calculate_hash(data, size, algo, value, &value_len)) {
77 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000078 algo, node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060079 return -EPROTONOSUPPORT;
Simon Glass25d6e6b2013-05-07 06:11:55 +000080 }
81
Simon Glassec539f72016-07-03 09:40:44 -060082 ret = fit_set_hash_value(fit, noffset, value, value_len);
83 if (ret) {
Simon Glass25d6e6b2013-05-07 06:11:55 +000084 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000085 node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060086 return ret;
Simon Glass25d6e6b2013-05-07 06:11:55 +000087 }
88
89 return 0;
90}
91
92/**
Simon Glassfbabc0f2013-06-13 15:10:01 -070093 * fit_image_write_sig() - write the signature to a FIT
Simon Glass10a1eca2013-05-07 06:11:54 +000094 *
Simon Glassfbabc0f2013-06-13 15:10:01 -070095 * This writes the signature and signer data to the FIT.
96 *
97 * @fit: pointer to the FIT format image header
98 * @noffset: hash node offset
99 * @value: signature value to be set
100 * @value_len: signature value length
101 * @comment: Text comment to write (NULL for none)
102 *
103 * returns
104 * 0, on success
105 * -FDT_ERR_..., on failure
106 */
107static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
108 int value_len, const char *comment, const char *region_prop,
109 int region_proplen)
110{
111 int string_size;
112 int ret;
113
114 /*
115 * Get the current string size, before we update the FIT and add
116 * more
117 */
118 string_size = fdt_size_dt_strings(fit);
119
120 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
121 if (!ret) {
122 ret = fdt_setprop_string(fit, noffset, "signer-name",
123 "mkimage");
124 }
125 if (!ret) {
126 ret = fdt_setprop_string(fit, noffset, "signer-version",
127 PLAIN_VERSION);
128 }
129 if (comment && !ret)
130 ret = fdt_setprop_string(fit, noffset, "comment", comment);
131 if (!ret)
132 ret = fit_set_timestamp(fit, noffset, time(NULL));
133 if (region_prop && !ret) {
134 uint32_t strdata[2];
135
136 ret = fdt_setprop(fit, noffset, "hashed-nodes",
137 region_prop, region_proplen);
138 strdata[0] = 0;
139 strdata[1] = cpu_to_fdt32(string_size);
140 if (!ret) {
141 ret = fdt_setprop(fit, noffset, "hashed-strings",
142 strdata, sizeof(strdata));
143 }
144 }
145
146 return ret;
147}
148
149static int fit_image_setup_sig(struct image_sign_info *info,
150 const char *keydir, void *fit, const char *image_name,
George McCollister23d14892017-01-06 13:14:17 -0600151 int noffset, const char *require_keys, const char *engine_id)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700152{
153 const char *node_name;
154 char *algo_name;
155
156 node_name = fit_get_name(fit, noffset, NULL);
157 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
158 printf("Can't get algo property for '%s' signature node in '%s' image node\n",
159 node_name, image_name);
160 return -1;
161 }
162
163 memset(info, '\0', sizeof(*info));
164 info->keydir = keydir;
165 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
166 info->fit = fit;
167 info->node_offset = noffset;
Masahiro Yamadac1f137b2017-10-27 13:25:21 +0900168 info->name = strdup(algo_name);
Andrew Duda6616c822016-11-08 18:53:41 +0000169 info->checksum = image_get_checksum_algo(algo_name);
170 info->crypto = image_get_crypto_algo(algo_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700171 info->require_keys = require_keys;
George McCollister23d14892017-01-06 13:14:17 -0600172 info->engine_id = engine_id;
Andrew Duda6616c822016-11-08 18:53:41 +0000173 if (!info->checksum || !info->crypto) {
Simon Glassfbabc0f2013-06-13 15:10:01 -0700174 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
175 algo_name, node_name, image_name);
176 return -1;
177 }
178
179 return 0;
180}
181
182/**
183 * fit_image_process_sig- Process a single subnode of the images/ node
184 *
185 * Check each subnode and process accordingly. For signature nodes we
186 * generate a signed hash of the supplised data and store it in the node.
187 *
188 * @keydir: Directory containing keys to use for signing
189 * @keydest: Destination FDT blob to write public keys into
190 * @fit: pointer to the FIT format image header
191 * @image_name: name of image being processes (used to display errors)
192 * @noffset: subnode offset
193 * @data: data to process
194 * @size: size of data in bytes
195 * @comment: Comment to add to signature nodes
196 * @require_keys: Mark all keys as 'required'
George McCollister23d14892017-01-06 13:14:17 -0600197 * @engine_id: Engine to use for signing
Simon Glassfbabc0f2013-06-13 15:10:01 -0700198 * @return 0 if ok, -1 on error
199 */
200static int fit_image_process_sig(const char *keydir, void *keydest,
201 void *fit, const char *image_name,
202 int noffset, const void *data, size_t size,
George McCollister23d14892017-01-06 13:14:17 -0600203 const char *comment, int require_keys, const char *engine_id)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700204{
205 struct image_sign_info info;
206 struct image_region region;
207 const char *node_name;
208 uint8_t *value;
209 uint value_len;
210 int ret;
211
212 if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
George McCollister23d14892017-01-06 13:14:17 -0600213 require_keys ? "image" : NULL, engine_id))
Simon Glassfbabc0f2013-06-13 15:10:01 -0700214 return -1;
215
216 node_name = fit_get_name(fit, noffset, NULL);
217 region.data = data;
218 region.size = size;
Andrew Duda6616c822016-11-08 18:53:41 +0000219 ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700220 if (ret) {
221 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
222 node_name, image_name, ret);
223
224 /* We allow keys to be missing */
225 if (ret == -ENOENT)
226 return 0;
227 return -1;
228 }
229
230 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
231 NULL, 0);
232 if (ret) {
Simon Glass802aa822014-06-02 22:04:53 -0600233 if (ret == -FDT_ERR_NOSPACE)
234 return -ENOSPC;
235 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
Simon Glassfbabc0f2013-06-13 15:10:01 -0700236 node_name, image_name, fdt_strerror(ret));
237 return -1;
238 }
239 free(value);
240
241 /* Get keyname again, as FDT has changed and invalidated our pointer */
242 info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
243
mario.six@gdsys.ccdc4053a2016-07-22 08:58:40 +0200244 /*
245 * Write the public key into the supplied FDT file; this might fail
mario.six@gdsys.ccd019e152016-07-19 11:07:06 +0200246 * several times, since we try signing with successively increasing
mario.six@gdsys.ccdc4053a2016-07-22 08:58:40 +0200247 * size values
248 */
Masahiro Yamada01486762017-10-27 15:04:20 +0900249 if (keydest) {
250 ret = info.crypto->add_verify_data(&info, keydest);
251 if (ret) {
252 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
253 node_name, image_name);
254 return ret;
255 }
256 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700257
258 return 0;
259}
260
261/**
262 * fit_image_add_verification_data() - calculate/set verig. data for image node
263 *
264 * This adds hash and signature values for an component image node.
Simon Glassee382652013-05-07 06:12:01 +0000265 *
266 * All existing hash subnodes are checked, if algorithm property is set to
267 * one of the supported hash algorithms, hash value is computed and
268 * corresponding hash node property is set, for example:
Simon Glass10a1eca2013-05-07 06:11:54 +0000269 *
270 * Input component image node structure:
271 *
Andre Przywara3234ecd2017-12-04 02:05:10 +0000272 * o image-1 (at image_noffset)
Simon Glass10a1eca2013-05-07 06:11:54 +0000273 * | - data = [binary data]
Andre Przywara3234ecd2017-12-04 02:05:10 +0000274 * o hash-1
Simon Glass10a1eca2013-05-07 06:11:54 +0000275 * |- algo = "sha1"
276 *
277 * Output component image node structure:
278 *
Andre Przywara3234ecd2017-12-04 02:05:10 +0000279 * o image-1 (at image_noffset)
Simon Glass10a1eca2013-05-07 06:11:54 +0000280 * | - data = [binary data]
Andre Przywara3234ecd2017-12-04 02:05:10 +0000281 * o hash-1
Simon Glass10a1eca2013-05-07 06:11:54 +0000282 * |- algo = "sha1"
283 * |- value = sha1(data)
284 *
Simon Glassee382652013-05-07 06:12:01 +0000285 * For signature details, please see doc/uImage.FIT/signature.txt
286 *
Simon Glassfbabc0f2013-06-13 15:10:01 -0700287 * @keydir Directory containing *.key and *.crt files (or NULL)
288 * @keydest FDT Blob to write public keys into (NULL if none)
Simon Glassee382652013-05-07 06:12:01 +0000289 * @fit: Pointer to the FIT format image header
290 * @image_noffset: Requested component image node
Simon Glassfbabc0f2013-06-13 15:10:01 -0700291 * @comment: Comment to add to signature nodes
292 * @require_keys: Mark all keys as 'required'
George McCollister23d14892017-01-06 13:14:17 -0600293 * @engine_id: Engine to use for signing
Simon Glassee382652013-05-07 06:12:01 +0000294 * @return: 0 on success, <0 on failure
Simon Glass10a1eca2013-05-07 06:11:54 +0000295 */
Simon Glassfbabc0f2013-06-13 15:10:01 -0700296int fit_image_add_verification_data(const char *keydir, void *keydest,
297 void *fit, int image_noffset, const char *comment,
George McCollister23d14892017-01-06 13:14:17 -0600298 int require_keys, const char *engine_id)
Simon Glass10a1eca2013-05-07 06:11:54 +0000299{
Simon Glassee382652013-05-07 06:12:01 +0000300 const char *image_name;
Simon Glass10a1eca2013-05-07 06:11:54 +0000301 const void *data;
302 size_t size;
Simon Glass10a1eca2013-05-07 06:11:54 +0000303 int noffset;
Simon Glass10a1eca2013-05-07 06:11:54 +0000304
305 /* Get image data and data length */
306 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
307 printf("Can't get image data/size\n");
308 return -1;
309 }
310
Simon Glass25d6e6b2013-05-07 06:11:55 +0000311 image_name = fit_get_name(fit, image_noffset, NULL);
312
Simon Glass10a1eca2013-05-07 06:11:54 +0000313 /* Process all hash subnodes of the component image node */
Simon Glassee382652013-05-07 06:12:01 +0000314 for (noffset = fdt_first_subnode(fit, image_noffset);
315 noffset >= 0;
316 noffset = fdt_next_subnode(fit, noffset)) {
317 const char *node_name;
318 int ret = 0;
319
320 /*
321 * Check subnode name, must be equal to "hash" or "signature".
322 * Multiple hash nodes require unique unit node
Andre Przywara3234ecd2017-12-04 02:05:10 +0000323 * names, e.g. hash-1, hash-2, signature-1, etc.
Simon Glassee382652013-05-07 06:12:01 +0000324 */
325 node_name = fit_get_name(fit, noffset, NULL);
326 if (!strncmp(node_name, FIT_HASH_NODENAME,
327 strlen(FIT_HASH_NODENAME))) {
328 ret = fit_image_process_hash(fit, image_name, noffset,
329 data, size);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700330 } else if (IMAGE_ENABLE_SIGN && keydir &&
331 !strncmp(node_name, FIT_SIG_NODENAME,
332 strlen(FIT_SIG_NODENAME))) {
333 ret = fit_image_process_sig(keydir, keydest,
334 fit, image_name, noffset, data, size,
George McCollister23d14892017-01-06 13:14:17 -0600335 comment, require_keys, engine_id);
Simon Glass10a1eca2013-05-07 06:11:54 +0000336 }
Simon Glassee382652013-05-07 06:12:01 +0000337 if (ret)
Simon Glassec539f72016-07-03 09:40:44 -0600338 return ret;
Simon Glassee382652013-05-07 06:12:01 +0000339 }
340
341 return 0;
342}
343
Simon Glass56ab8d62013-06-13 15:10:09 -0700344struct strlist {
345 int count;
346 char **strings;
347};
348
349static void strlist_init(struct strlist *list)
350{
351 memset(list, '\0', sizeof(*list));
352}
353
354static void strlist_free(struct strlist *list)
355{
356 int i;
357
358 for (i = 0; i < list->count; i++)
359 free(list->strings[i]);
360 free(list->strings);
361}
362
363static int strlist_add(struct strlist *list, const char *str)
364{
365 char *dup;
366
367 dup = strdup(str);
368 list->strings = realloc(list->strings,
369 (list->count + 1) * sizeof(char *));
370 if (!list || !str)
371 return -1;
372 list->strings[list->count++] = dup;
373
374 return 0;
375}
376
377static const char *fit_config_get_image_list(void *fit, int noffset,
378 int *lenp, int *allow_missingp)
379{
380 static const char default_list[] = FIT_KERNEL_PROP "\0"
381 FIT_FDT_PROP;
382 const char *prop;
383
384 /* If there is an "image" property, use that */
385 prop = fdt_getprop(fit, noffset, "sign-images", lenp);
386 if (prop) {
387 *allow_missingp = 0;
388 return *lenp ? prop : NULL;
389 }
390
391 /* Default image list */
392 *allow_missingp = 1;
393 *lenp = sizeof(default_list);
394
395 return default_list;
396}
397
398static int fit_config_get_hash_list(void *fit, int conf_noffset,
399 int sig_offset, struct strlist *node_inc)
400{
401 int allow_missing;
402 const char *prop, *iname, *end;
403 const char *conf_name, *sig_name;
404 char name[200], path[200];
405 int image_count;
406 int ret, len;
407
408 conf_name = fit_get_name(fit, conf_noffset, NULL);
409 sig_name = fit_get_name(fit, sig_offset, NULL);
410
411 /*
412 * Build a list of nodes we need to hash. We always need the root
413 * node and the configuration.
414 */
415 strlist_init(node_inc);
416 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
417 if (strlist_add(node_inc, "/") ||
418 strlist_add(node_inc, name))
419 goto err_mem;
420
421 /* Get a list of images that we intend to sign */
Heiko Schocherfdfd5202014-03-03 12:19:23 +0100422 prop = fit_config_get_image_list(fit, sig_offset, &len,
Simon Glass56ab8d62013-06-13 15:10:09 -0700423 &allow_missing);
424 if (!prop)
425 return 0;
426
427 /* Locate the images */
428 end = prop + len;
429 image_count = 0;
430 for (iname = prop; iname < end; iname += strlen(iname) + 1) {
431 int noffset;
432 int image_noffset;
433 int hash_count;
434
435 image_noffset = fit_conf_get_prop_node(fit, conf_noffset,
436 iname);
437 if (image_noffset < 0) {
438 printf("Failed to find image '%s' in configuration '%s/%s'\n",
439 iname, conf_name, sig_name);
440 if (allow_missing)
441 continue;
442
443 return -ENOENT;
444 }
445
446 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
447 if (ret < 0)
448 goto err_path;
449 if (strlist_add(node_inc, path))
450 goto err_mem;
451
452 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
453 conf_name);
454
455 /* Add all this image's hashes */
456 hash_count = 0;
457 for (noffset = fdt_first_subnode(fit, image_noffset);
458 noffset >= 0;
459 noffset = fdt_next_subnode(fit, noffset)) {
460 const char *name = fit_get_name(fit, noffset, NULL);
461
462 if (strncmp(name, FIT_HASH_NODENAME,
463 strlen(FIT_HASH_NODENAME)))
464 continue;
465 ret = fdt_get_path(fit, noffset, path, sizeof(path));
466 if (ret < 0)
467 goto err_path;
468 if (strlist_add(node_inc, path))
469 goto err_mem;
470 hash_count++;
471 }
472
473 if (!hash_count) {
474 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
475 conf_name, sig_name, iname);
476 return -ENOMSG;
477 }
478
479 image_count++;
480 }
481
482 if (!image_count) {
483 printf("Failed to find any images for configuration '%s/%s'\n",
484 conf_name, sig_name);
485 return -ENOMSG;
486 }
487
488 return 0;
489
490err_mem:
491 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
492 sig_name);
493 return -ENOMEM;
494
495err_path:
496 printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
497 iname, conf_name, sig_name, fdt_strerror(ret));
498 return -ENOENT;
499}
500
501static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
502 struct image_region **regionp, int *region_countp,
503 char **region_propp, int *region_proplen)
504{
505 char * const exc_prop[] = {"data"};
506 struct strlist node_inc;
507 struct image_region *region;
508 struct fdt_region fdt_regions[100];
509 const char *conf_name, *sig_name;
510 char path[200];
511 int count, i;
512 char *region_prop;
513 int ret, len;
514
515 conf_name = fit_get_name(fit, conf_noffset, NULL);
Masahiro Yamada2fc6b6f2017-10-19 19:16:21 +0900516 sig_name = fit_get_name(fit, noffset, NULL);
Simon Glass56ab8d62013-06-13 15:10:09 -0700517 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
518
519 /* Get a list of nodes we want to hash */
520 ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
521 if (ret)
522 return ret;
523
524 /* Get a list of regions to hash */
525 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
526 exc_prop, ARRAY_SIZE(exc_prop),
527 fdt_regions, ARRAY_SIZE(fdt_regions),
528 path, sizeof(path), 1);
529 if (count < 0) {
530 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
531 sig_name, fdt_strerror(ret));
532 return -EIO;
533 }
534 if (count == 0) {
535 printf("No data to hash for configuration '%s/%s': %s\n",
536 conf_name, sig_name, fdt_strerror(ret));
537 return -EINVAL;
538 }
539
540 /* Build our list of data blocks */
541 region = fit_region_make_list(fit, fdt_regions, count, NULL);
542 if (!region) {
543 printf("Out of memory hashing configuration '%s/%s'\n",
544 conf_name, sig_name);
545 return -ENOMEM;
546 }
547
548 /* Create a list of all hashed properties */
549 debug("Hash nodes:\n");
550 for (i = len = 0; i < node_inc.count; i++) {
551 debug(" %s\n", node_inc.strings[i]);
552 len += strlen(node_inc.strings[i]) + 1;
553 }
554 region_prop = malloc(len);
555 if (!region_prop) {
556 printf("Out of memory setting up regions for configuration '%s/%s'\n",
557 conf_name, sig_name);
558 return -ENOMEM;
559 }
560 for (i = len = 0; i < node_inc.count;
561 len += strlen(node_inc.strings[i]) + 1, i++)
562 strcpy(region_prop + len, node_inc.strings[i]);
563 strlist_free(&node_inc);
564
565 *region_countp = count;
566 *regionp = region;
567 *region_propp = region_prop;
568 *region_proplen = len;
569
570 return 0;
571}
572
573static int fit_config_process_sig(const char *keydir, void *keydest,
574 void *fit, const char *conf_name, int conf_noffset,
George McCollister23d14892017-01-06 13:14:17 -0600575 int noffset, const char *comment, int require_keys,
576 const char *engine_id)
Simon Glass56ab8d62013-06-13 15:10:09 -0700577{
578 struct image_sign_info info;
579 const char *node_name;
580 struct image_region *region;
581 char *region_prop;
582 int region_proplen;
583 int region_count;
584 uint8_t *value;
585 uint value_len;
586 int ret;
587
588 node_name = fit_get_name(fit, noffset, NULL);
589 if (fit_config_get_data(fit, conf_noffset, noffset, &region,
590 &region_count, &region_prop, &region_proplen))
591 return -1;
592
593 if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
George McCollister23d14892017-01-06 13:14:17 -0600594 require_keys ? "conf" : NULL, engine_id))
Simon Glass56ab8d62013-06-13 15:10:09 -0700595 return -1;
596
Andrew Duda6616c822016-11-08 18:53:41 +0000597 ret = info.crypto->sign(&info, region, region_count, &value,
598 &value_len);
Simon Glass56ab8d62013-06-13 15:10:09 -0700599 free(region);
600 if (ret) {
601 printf("Failed to sign '%s' signature node in '%s' conf node\n",
602 node_name, conf_name);
603
604 /* We allow keys to be missing */
605 if (ret == -ENOENT)
606 return 0;
607 return -1;
608 }
609
Simon Glass802aa822014-06-02 22:04:53 -0600610 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
611 region_prop, region_proplen);
612 if (ret) {
613 if (ret == -FDT_ERR_NOSPACE)
614 return -ENOSPC;
615 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
616 node_name, conf_name, fdt_strerror(ret));
Simon Glass56ab8d62013-06-13 15:10:09 -0700617 return -1;
618 }
619 free(value);
620 free(region_prop);
621
622 /* Get keyname again, as FDT has changed and invalidated our pointer */
623 info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
624
625 /* Write the public key into the supplied FDT file */
Simon Glass802aa822014-06-02 22:04:53 -0600626 if (keydest) {
Andrew Duda6616c822016-11-08 18:53:41 +0000627 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glass802aa822014-06-02 22:04:53 -0600628 if (ret) {
Masahiro Yamadafe391a72017-10-27 15:04:21 +0900629 printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
Simon Glass802aa822014-06-02 22:04:53 -0600630 node_name, conf_name);
Simon Glass802aa822014-06-02 22:04:53 -0600631 }
Simon Glass9423bad2014-06-12 07:24:42 -0600632 return ret;
Simon Glass56ab8d62013-06-13 15:10:09 -0700633 }
634
635 return 0;
636}
637
638static int fit_config_add_verification_data(const char *keydir, void *keydest,
639 void *fit, int conf_noffset, const char *comment,
George McCollister23d14892017-01-06 13:14:17 -0600640 int require_keys, const char *engine_id)
Simon Glass56ab8d62013-06-13 15:10:09 -0700641{
642 const char *conf_name;
643 int noffset;
644
645 conf_name = fit_get_name(fit, conf_noffset, NULL);
646
647 /* Process all hash subnodes of the configuration node */
648 for (noffset = fdt_first_subnode(fit, conf_noffset);
649 noffset >= 0;
650 noffset = fdt_next_subnode(fit, noffset)) {
651 const char *node_name;
652 int ret = 0;
653
654 node_name = fit_get_name(fit, noffset, NULL);
655 if (!strncmp(node_name, FIT_SIG_NODENAME,
656 strlen(FIT_SIG_NODENAME))) {
657 ret = fit_config_process_sig(keydir, keydest,
658 fit, conf_name, conf_noffset, noffset, comment,
George McCollister23d14892017-01-06 13:14:17 -0600659 require_keys, engine_id);
Simon Glass56ab8d62013-06-13 15:10:09 -0700660 }
661 if (ret)
662 return ret;
663 }
664
665 return 0;
666}
667
Simon Glassfbabc0f2013-06-13 15:10:01 -0700668int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
George McCollister23d14892017-01-06 13:14:17 -0600669 const char *comment, int require_keys,
670 const char *engine_id)
Simon Glassee382652013-05-07 06:12:01 +0000671{
Simon Glass56ab8d62013-06-13 15:10:09 -0700672 int images_noffset, confs_noffset;
Simon Glassee382652013-05-07 06:12:01 +0000673 int noffset;
674 int ret;
675
676 /* Find images parent node offset */
677 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
678 if (images_noffset < 0) {
679 printf("Can't find images parent node '%s' (%s)\n",
680 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
681 return images_noffset;
682 }
683
684 /* Process its subnodes, print out component images details */
685 for (noffset = fdt_first_subnode(fit, images_noffset);
686 noffset >= 0;
687 noffset = fdt_next_subnode(fit, noffset)) {
688 /*
689 * Direct child node of the images parent node,
690 * i.e. component image node.
691 */
Simon Glassfbabc0f2013-06-13 15:10:01 -0700692 ret = fit_image_add_verification_data(keydir, keydest,
George McCollister23d14892017-01-06 13:14:17 -0600693 fit, noffset, comment, require_keys, engine_id);
Simon Glassee382652013-05-07 06:12:01 +0000694 if (ret)
695 return ret;
Simon Glass10a1eca2013-05-07 06:11:54 +0000696 }
697
Simon Glass56ab8d62013-06-13 15:10:09 -0700698 /* If there are no keys, we can't sign configurations */
699 if (!IMAGE_ENABLE_SIGN || !keydir)
700 return 0;
701
702 /* Find configurations parent node offset */
703 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
704 if (confs_noffset < 0) {
705 printf("Can't find images parent node '%s' (%s)\n",
Heiko Schocher441f48d2014-08-11 11:02:17 +0200706 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Simon Glass56ab8d62013-06-13 15:10:09 -0700707 return -ENOENT;
708 }
709
710 /* Process its subnodes, print out component images details */
711 for (noffset = fdt_first_subnode(fit, confs_noffset);
712 noffset >= 0;
713 noffset = fdt_next_subnode(fit, noffset)) {
714 ret = fit_config_add_verification_data(keydir, keydest,
715 fit, noffset, comment,
George McCollister23d14892017-01-06 13:14:17 -0600716 require_keys,
717 engine_id);
Simon Glass56ab8d62013-06-13 15:10:09 -0700718 if (ret)
719 return ret;
720 }
721
Simon Glass10a1eca2013-05-07 06:11:54 +0000722 return 0;
723}
Heiko Schocherd7b42322014-03-03 12:19:30 +0100724
725#ifdef CONFIG_FIT_SIGNATURE
Simon Glassa51991d2014-06-12 07:24:53 -0600726int fit_check_sign(const void *fit, const void *key)
Heiko Schocherd7b42322014-03-03 12:19:30 +0100727{
728 int cfg_noffset;
729 int ret;
730
Simon Glassa51991d2014-06-12 07:24:53 -0600731 cfg_noffset = fit_conf_get_node(fit, NULL);
Heiko Schocherd7b42322014-03-03 12:19:30 +0100732 if (!cfg_noffset)
733 return -1;
734
Simon Glassa51991d2014-06-12 07:24:53 -0600735 printf("Verifying Hash Integrity ... ");
736 ret = fit_config_verify(fit, cfg_noffset);
737 if (ret)
738 return ret;
739 ret = bootm_host_load_images(fit, cfg_noffset);
740
Heiko Schocherd7b42322014-03-03 12:19:30 +0100741 return ret;
742}
743#endif