]> git.ipfire.org Git - thirdparty/u-boot.git/blame - tools/image-host.c
riscv: qemu: imply GOLDFISH_RTC
[thirdparty/u-boot.git] / tools / image-host.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
604f23dd
SG
2/*
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.
604f23dd
SG
9 */
10
11#include "mkimage.h"
ce1400f6 12#include <bootm.h>
64045a6a 13#include <fdt_region.h>
604f23dd 14#include <image.h>
56518e71 15#include <version.h>
604f23dd 16
03e59826 17#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
6e052d1c
PR
18#include <openssl/pem.h>
19#include <openssl/evp.h>
03e59826 20#endif
6e052d1c 21
b7260910
SG
22/**
23 * fit_set_hash_value - set hash value in requested has node
24 * @fit: pointer to the FIT format image header
25 * @noffset: hash node offset
26 * @value: hash value to be set
27 * @value_len: hash value length
28 *
29 * fit_set_hash_value() attempts to set hash value in a node at offset
30 * given and returns operation status to the caller.
31 *
32 * returns
33 * 0, on success
34 * -1, on failure
35 */
36static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
37 int value_len)
38{
39 int ret;
40
41 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
42 if (ret) {
8d8851e8
OS
43 fprintf(stderr, "Can't set hash '%s' property for '%s' node(%s)\n",
44 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
45 fdt_strerror(ret));
1152a05e 46 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
b7260910
SG
47 }
48
49 return 0;
50}
51
94e5fa46
SG
52/**
53 * fit_image_process_hash - Process a single subnode of the images/ node
54 *
55 * Check each subnode and process accordingly. For hash nodes we generate
70e6bcc4 56 * a hash of the supplied data and store it in the node.
94e5fa46
SG
57 *
58 * @fit: pointer to the FIT format image header
70e6bcc4 59 * @image_name: name of image being processed (used to display errors)
94e5fa46
SG
60 * @noffset: subnode offset
61 * @data: data to process
62 * @size: size of data in bytes
185f812c 63 * Return: 0 if ok, -1 on error
94e5fa46
SG
64 */
65static int fit_image_process_hash(void *fit, const char *image_name,
66 int noffset, const void *data, size_t size)
67{
68 uint8_t value[FIT_MAX_HASH_LEN];
bbb467dc 69 const char *node_name;
94e5fa46 70 int value_len;
4550ce9b 71 const char *algo;
1152a05e 72 int ret;
94e5fa46 73
bbb467dc 74 node_name = fit_get_name(fit, noffset, NULL);
94e5fa46
SG
75
76 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
8d8851e8
OS
77 fprintf(stderr,
78 "Can't get hash algo property for '%s' hash node in '%s' image node\n",
79 node_name, image_name);
1152a05e 80 return -ENOENT;
94e5fa46
SG
81 }
82
83 if (calculate_hash(data, size, algo, value, &value_len)) {
8d8851e8
OS
84 fprintf(stderr,
85 "Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
86 algo, node_name, image_name);
1152a05e 87 return -EPROTONOSUPPORT;
94e5fa46
SG
88 }
89
1152a05e
SG
90 ret = fit_set_hash_value(fit, noffset, value, value_len);
91 if (ret) {
8d8851e8
OS
92 fprintf(stderr, "Can't set hash value for '%s' hash node in '%s' image node\n",
93 node_name, image_name);
1152a05e 94 return ret;
94e5fa46
SG
95 }
96
97 return 0;
98}
99
604f23dd 100/**
56518e71 101 * fit_image_write_sig() - write the signature to a FIT
604f23dd 102 *
56518e71
SG
103 * This writes the signature and signer data to the FIT.
104 *
105 * @fit: pointer to the FIT format image header
106 * @noffset: hash node offset
107 * @value: signature value to be set
108 * @value_len: signature value length
109 * @comment: Text comment to write (NULL for none)
110 *
111 * returns
112 * 0, on success
113 * -FDT_ERR_..., on failure
114 */
115static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
116 int value_len, const char *comment, const char *region_prop,
5902a397 117 int region_proplen, const char *cmdname, const char *algo_name)
56518e71
SG
118{
119 int string_size;
120 int ret;
121
122 /*
123 * Get the current string size, before we update the FIT and add
124 * more
125 */
126 string_size = fdt_size_dt_strings(fit);
127
128 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
129 if (!ret) {
130 ret = fdt_setprop_string(fit, noffset, "signer-name",
131 "mkimage");
132 }
133 if (!ret) {
134 ret = fdt_setprop_string(fit, noffset, "signer-version",
135 PLAIN_VERSION);
136 }
137 if (comment && !ret)
138 ret = fdt_setprop_string(fit, noffset, "comment", comment);
795f452e
AK
139 if (!ret) {
140 time_t timestamp = imagetool_get_source_date(cmdname,
141 time(NULL));
7c39799d 142 uint32_t t = cpu_to_uimage(timestamp);
795f452e 143
7c39799d
ML
144 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
145 sizeof(uint32_t));
795f452e 146 }
56518e71
SG
147 if (region_prop && !ret) {
148 uint32_t strdata[2];
149
150 ret = fdt_setprop(fit, noffset, "hashed-nodes",
151 region_prop, region_proplen);
7346c1e1 152 /* This is a legacy offset, it is unused, and must remain 0. */
56518e71
SG
153 strdata[0] = 0;
154 strdata[1] = cpu_to_fdt32(string_size);
155 if (!ret) {
156 ret = fdt_setprop(fit, noffset, "hashed-strings",
157 strdata, sizeof(strdata));
158 }
159 }
5902a397
JK
160 if (algo_name && !ret)
161 ret = fdt_setprop_string(fit, noffset, "algo", algo_name);
56518e71
SG
162
163 return ret;
164}
165
166static int fit_image_setup_sig(struct image_sign_info *info,
36bfcb62
AG
167 const char *keydir, const char *keyfile, void *fit,
168 const char *image_name, int noffset, const char *require_keys,
5902a397 169 const char *engine_id, const char *algo_name)
56518e71
SG
170{
171 const char *node_name;
20031567 172 const char *padding_name;
56518e71
SG
173
174 node_name = fit_get_name(fit, noffset, NULL);
5902a397
JK
175 if (!algo_name) {
176 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
8d8851e8
OS
177 fprintf(stderr,
178 "Can't get algo property for '%s' signature node in '%s' image node\n",
179 node_name, image_name);
5902a397
JK
180 return -1;
181 }
56518e71
SG
182 }
183
20031567
PR
184 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
185
56518e71
SG
186 memset(info, '\0', sizeof(*info));
187 info->keydir = keydir;
36bfcb62 188 info->keyfile = keyfile;
72188f54 189 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
56518e71
SG
190 info->fit = fit;
191 info->node_offset = noffset;
1d88a99d 192 info->name = strdup(algo_name);
83dd98e0
AD
193 info->checksum = image_get_checksum_algo(algo_name);
194 info->crypto = image_get_crypto_algo(algo_name);
20031567 195 info->padding = image_get_padding_algo(padding_name);
56518e71 196 info->require_keys = require_keys;
f1ca1fde 197 info->engine_id = engine_id;
83dd98e0 198 if (!info->checksum || !info->crypto) {
8d8851e8
OS
199 fprintf(stderr,
200 "Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
201 algo_name, node_name, image_name);
56518e71
SG
202 return -1;
203 }
204
205 return 0;
206}
207
208/**
209 * fit_image_process_sig- Process a single subnode of the images/ node
210 *
211 * Check each subnode and process accordingly. For signature nodes we
70e6bcc4 212 * generate a signed hash of the supplied data and store it in the node.
56518e71
SG
213 *
214 * @keydir: Directory containing keys to use for signing
70e6bcc4 215 * @keydest: Destination FDT blob to write public keys into (NULL if none)
56518e71 216 * @fit: pointer to the FIT format image header
70e6bcc4 217 * @image_name: name of image being processed (used to display errors)
56518e71
SG
218 * @noffset: subnode offset
219 * @data: data to process
220 * @size: size of data in bytes
221 * @comment: Comment to add to signature nodes
222 * @require_keys: Mark all keys as 'required'
f1ca1fde 223 * @engine_id: Engine to use for signing
9737c2d1
SG
224 * Return: keydest node if @keydest is non-NULL, else 0 if none; -ve error code
225 * on failure
56518e71 226 */
36bfcb62
AG
227static int fit_image_process_sig(const char *keydir, const char *keyfile,
228 void *keydest, void *fit, const char *image_name,
56518e71 229 int noffset, const void *data, size_t size,
795f452e 230 const char *comment, int require_keys, const char *engine_id,
5902a397 231 const char *cmdname, const char *algo_name)
56518e71
SG
232{
233 struct image_sign_info info;
234 struct image_region region;
235 const char *node_name;
236 uint8_t *value;
237 uint value_len;
238 int ret;
239
36bfcb62
AG
240 if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
241 noffset, require_keys ? "image" : NULL,
5902a397 242 engine_id, algo_name))
56518e71
SG
243 return -1;
244
245 node_name = fit_get_name(fit, noffset, NULL);
246 region.data = data;
247 region.size = size;
83dd98e0 248 ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
56518e71 249 if (ret) {
8d8851e8
OS
250 fprintf(stderr, "Failed to sign '%s' signature node in '%s' image node: %d\n",
251 node_name, image_name, ret);
56518e71
SG
252
253 /* We allow keys to be missing */
254 if (ret == -ENOENT)
255 return 0;
256 return -1;
257 }
258
259 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
5902a397 260 NULL, 0, cmdname, algo_name);
56518e71 261 if (ret) {
a9468115
SG
262 if (ret == -FDT_ERR_NOSPACE)
263 return -ENOSPC;
8d8851e8
OS
264 fprintf(stderr,
265 "Can't write signature for '%s' signature node in '%s' conf node: %s\n",
266 node_name, image_name, fdt_strerror(ret));
56518e71
SG
267 return -1;
268 }
269 free(value);
270
271 /* Get keyname again, as FDT has changed and invalidated our pointer */
72188f54 272 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
56518e71 273
713fb2dc 274 /*
275 * Write the public key into the supplied FDT file; this might fail
c236ebd2 276 * several times, since we try signing with successively increasing
713fb2dc 277 * size values
278 */
6793d017
MY
279 if (keydest) {
280 ret = info.crypto->add_verify_data(&info, keydest);
c033dc8c 281 if (ret < 0) {
8d8851e8
OS
282 fprintf(stderr,
283 "Failed to add verification data for '%s' signature node in '%s' image node\n",
284 node_name, image_name);
6793d017
MY
285 return ret;
286 }
9737c2d1
SG
287 /* Return the node that was written to */
288 return ret;
6793d017 289 }
56518e71
SG
290
291 return 0;
292}
293
7298e422
PR
294static int fit_image_read_data(char *filename, unsigned char *data,
295 int expected_size)
296{
297 struct stat sbuf;
298 int fd, ret = -1;
299 ssize_t n;
300
301 /* Open file */
302 fd = open(filename, O_RDONLY | O_BINARY);
303 if (fd < 0) {
8d8851e8
OS
304 fprintf(stderr, "Can't open file %s (err=%d => %s)\n",
305 filename, errno, strerror(errno));
7298e422
PR
306 return -1;
307 }
308
309 /* Compute file size */
310 if (fstat(fd, &sbuf) < 0) {
8d8851e8
OS
311 fprintf(stderr, "Can't fstat file %s (err=%d => %s)\n",
312 filename, errno, strerror(errno));
7298e422
PR
313 goto err;
314 }
315
316 /* Check file size */
317 if (sbuf.st_size != expected_size) {
8d8851e8
OS
318 fprintf(stderr, "File %s don't have the expected size (size=%lld, expected=%d)\n",
319 filename, (long long)sbuf.st_size, expected_size);
7298e422
PR
320 goto err;
321 }
322
323 /* Read data */
324 n = read(fd, data, sbuf.st_size);
325 if (n < 0) {
8d8851e8
OS
326 fprintf(stderr, "Can't read file %s (err=%d => %s)\n",
327 filename, errno, strerror(errno));
7298e422
PR
328 goto err;
329 }
330
331 /* Check that we have read all the file */
332 if (n != sbuf.st_size) {
8d8851e8
OS
333 fprintf(stderr, "Can't read all file %s (read %zd bytes, expected %lld)\n",
334 filename, n, (long long)sbuf.st_size);
7298e422
PR
335 goto err;
336 }
337
338 ret = 0;
339
340err:
341 close(fd);
342 return ret;
343}
344
a6982a6f
PR
345static int get_random_data(void *data, int size)
346{
347 unsigned char *tmp = data;
348 struct timespec date;
7f0f4e18 349 int i, ret;
a6982a6f
PR
350
351 if (!tmp) {
8d8851e8 352 fprintf(stderr, "%s: pointer data is NULL\n", __func__);
a6982a6f
PR
353 ret = -1;
354 goto out;
355 }
356
357 ret = clock_gettime(CLOCK_MONOTONIC, &date);
7f0f4e18 358 if (ret) {
8d8851e8
OS
359 fprintf(stderr, "%s: clock_gettime has failed (%s)\n", __func__,
360 strerror(errno));
a6982a6f
PR
361 goto out;
362 }
363
cc34f04e 364 srandom(date.tv_nsec);
a6982a6f
PR
365
366 for (i = 0; i < size; i++) {
cc34f04e 367 *tmp = random() & 0xff;
a6982a6f
PR
368 tmp++;
369 }
370
371 out:
372 return ret;
373}
374
7298e422
PR
375static int fit_image_setup_cipher(struct image_cipher_info *info,
376 const char *keydir, void *fit,
377 const char *image_name, int image_noffset,
c520266f 378 int noffset)
7298e422
PR
379{
380 char *algo_name;
381 char filename[128];
382 int ret = -1;
383
384 if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
8d8851e8
OS
385 fprintf(stderr, "Can't get algo name for cipher in image '%s'\n",
386 image_name);
7298e422
PR
387 goto out;
388 }
389
390 info->keydir = keydir;
391
392 /* Read the key name */
72188f54 393 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
7298e422 394 if (!info->keyname) {
8d8851e8
OS
395 fprintf(stderr, "Can't get key name for cipher in image '%s'\n",
396 image_name);
7298e422
PR
397 goto out;
398 }
399
a6982a6f
PR
400 /*
401 * Read the IV name
402 *
403 * If this property is not provided then mkimage will generate
404 * a random IV and store it in the FIT image
405 */
7298e422 406 info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
7298e422
PR
407
408 info->fit = fit;
409 info->node_noffset = noffset;
410 info->name = algo_name;
411
412 info->cipher = image_get_cipher_algo(algo_name);
413 if (!info->cipher) {
8d8851e8 414 fprintf(stderr, "Can't get algo for cipher '%s'\n", image_name);
7298e422
PR
415 goto out;
416 }
417
418 /* Read the key in the file */
419 snprintf(filename, sizeof(filename), "%s/%s%s",
420 info->keydir, info->keyname, ".bin");
421 info->key = malloc(info->cipher->key_len);
422 if (!info->key) {
8d8851e8 423 fprintf(stderr, "Can't allocate memory for key\n");
7298e422
PR
424 ret = -1;
425 goto out;
426 }
427 ret = fit_image_read_data(filename, (unsigned char *)info->key,
428 info->cipher->key_len);
429 if (ret < 0)
430 goto out;
431
7298e422
PR
432 info->iv = malloc(info->cipher->iv_len);
433 if (!info->iv) {
8d8851e8 434 fprintf(stderr, "Can't allocate memory for iv\n");
7298e422
PR
435 ret = -1;
436 goto out;
437 }
a6982a6f
PR
438
439 if (info->ivname) {
440 /* Read the IV in the file */
441 snprintf(filename, sizeof(filename), "%s/%s%s",
442 info->keydir, info->ivname, ".bin");
443 ret = fit_image_read_data(filename, (unsigned char *)info->iv,
444 info->cipher->iv_len);
445 } else {
446 /* Generate an ramdom IV */
447 ret = get_random_data((void *)info->iv, info->cipher->iv_len);
448 }
7298e422
PR
449
450 out:
451 return ret;
452}
453
454int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
455 const void *data, size_t size,
456 unsigned char *data_ciphered, int data_ciphered_len)
457{
458 int ret = -1;
459
04aeebb1 460 /* Replace data with ciphered data */
7298e422
PR
461 ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
462 data_ciphered, data_ciphered_len);
04aeebb1
PO
463 if (ret == -FDT_ERR_NOSPACE) {
464 ret = -ENOSPC;
465 goto out;
466 }
7298e422 467 if (ret) {
8d8851e8 468 fprintf(stderr, "Can't replace data with ciphered data (err = %d)\n", ret);
7298e422
PR
469 goto out;
470 }
471
472 /* add non ciphered data size */
473 ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
04aeebb1
PO
474 if (ret == -FDT_ERR_NOSPACE) {
475 ret = -ENOSPC;
476 goto out;
477 }
7298e422 478 if (ret) {
8d8851e8 479 fprintf(stderr, "Can't add unciphered data size (err = %d)\n", ret);
7298e422
PR
480 goto out;
481 }
482
483 out:
484 return ret;
485}
486
487static int
488fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
489 const char *image_name, int image_noffset,
c520266f 490 int node_noffset, const void *data, size_t size,
7298e422
PR
491 const char *cmdname)
492{
493 struct image_cipher_info info;
494 unsigned char *data_ciphered = NULL;
495 int data_ciphered_len;
496 int ret;
497
498 memset(&info, 0, sizeof(info));
499
500 ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
c520266f 501 image_noffset, node_noffset);
7298e422
PR
502 if (ret)
503 goto out;
504
505 ret = info.cipher->encrypt(&info, data, size,
506 &data_ciphered, &data_ciphered_len);
507 if (ret)
508 goto out;
509
510 /*
511 * Write the public key into the supplied FDT file; this might fail
512 * several times, since we try signing with successively increasing
513 * size values
a6982a6f 514 * And, if needed, write the iv in the FIT file
7298e422
PR
515 */
516 if (keydest) {
a6982a6f 517 ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
7298e422 518 if (ret) {
8d8851e8
OS
519 fprintf(stderr,
520 "Failed to add verification data for cipher '%s' in image '%s'\n",
521 info.keyname, image_name);
7298e422
PR
522 goto out;
523 }
524 }
525
526 ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
527 data, size,
528 data_ciphered, data_ciphered_len);
529
530 out:
531 free(data_ciphered);
532 free((void *)info.key);
533 free((void *)info.iv);
534 return ret;
535}
536
537int fit_image_cipher_data(const char *keydir, void *keydest,
538 void *fit, int image_noffset, const char *comment,
539 int require_keys, const char *engine_id,
540 const char *cmdname)
541{
542 const char *image_name;
543 const void *data;
544 size_t size;
b33e5cc1 545 int cipher_node_offset, len;
7298e422
PR
546
547 /* Get image name */
548 image_name = fit_get_name(fit, image_noffset, NULL);
549 if (!image_name) {
8d8851e8 550 fprintf(stderr, "Can't get image name\n");
7298e422
PR
551 return -1;
552 }
553
554 /* Get image data and data length */
555 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
8d8851e8 556 fprintf(stderr, "Can't get image data/size\n");
7298e422
PR
557 return -1;
558 }
559
b33e5cc1
PO
560 /*
561 * Don't cipher ciphered data.
562 *
563 * If the data-size-unciphered property is present the data for this
564 * image is already encrypted. This is important as 'mkimage -F' can be
565 * run multiple times on a FIT image.
566 */
567 if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len))
568 return 0;
569 if (len != -FDT_ERR_NOTFOUND) {
8d8851e8 570 fprintf(stderr, "Failure testing for data-size-unciphered\n");
b33e5cc1
PO
571 return -1;
572 }
7298e422 573
c520266f
PO
574 /* Process cipher node if present */
575 cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
576 FIT_CIPHER_NODENAME);
577 if (cipher_node_offset == -FDT_ERR_NOTFOUND)
578 return 0;
579 if (cipher_node_offset < 0) {
8d8851e8 580 fprintf(stderr, "Failure getting cipher node\n");
c520266f 581 return -1;
7298e422 582 }
c520266f
PO
583 if (!IMAGE_ENABLE_ENCRYPT || !keydir)
584 return 0;
585 return fit_image_process_cipher(keydir, keydest, fit, image_name,
586 image_noffset, cipher_node_offset, data, size, cmdname);
7298e422
PR
587}
588
56518e71
SG
589/**
590 * fit_image_add_verification_data() - calculate/set verig. data for image node
591 *
592 * This adds hash and signature values for an component image node.
bbb467dc
SG
593 *
594 * All existing hash subnodes are checked, if algorithm property is set to
595 * one of the supported hash algorithms, hash value is computed and
596 * corresponding hash node property is set, for example:
604f23dd
SG
597 *
598 * Input component image node structure:
599 *
b2267e8a 600 * o image-1 (at image_noffset)
604f23dd 601 * | - data = [binary data]
b2267e8a 602 * o hash-1
604f23dd
SG
603 * |- algo = "sha1"
604 *
605 * Output component image node structure:
606 *
b2267e8a 607 * o image-1 (at image_noffset)
604f23dd 608 * | - data = [binary data]
b2267e8a 609 * o hash-1
604f23dd
SG
610 * |- algo = "sha1"
611 * |- value = sha1(data)
612 *
bbb467dc
SG
613 * For signature details, please see doc/uImage.FIT/signature.txt
614 *
56518e71
SG
615 * @keydir Directory containing *.key and *.crt files (or NULL)
616 * @keydest FDT Blob to write public keys into (NULL if none)
bbb467dc
SG
617 * @fit: Pointer to the FIT format image header
618 * @image_noffset: Requested component image node
56518e71
SG
619 * @comment: Comment to add to signature nodes
620 * @require_keys: Mark all keys as 'required'
f1ca1fde 621 * @engine_id: Engine to use for signing
bbb467dc 622 * @return: 0 on success, <0 on failure
604f23dd 623 */
36bfcb62
AG
624int fit_image_add_verification_data(const char *keydir, const char *keyfile,
625 void *keydest, void *fit, int image_noffset,
626 const char *comment, int require_keys, const char *engine_id,
5902a397 627 const char *cmdname, const char* algo_name)
604f23dd 628{
bbb467dc 629 const char *image_name;
604f23dd
SG
630 const void *data;
631 size_t size;
604f23dd 632 int noffset;
604f23dd
SG
633
634 /* Get image data and data length */
635 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
8d8851e8 636 fprintf(stderr, "Can't get image data/size\n");
604f23dd
SG
637 return -1;
638 }
639
94e5fa46
SG
640 image_name = fit_get_name(fit, image_noffset, NULL);
641
604f23dd 642 /* Process all hash subnodes of the component image node */
bbb467dc
SG
643 for (noffset = fdt_first_subnode(fit, image_noffset);
644 noffset >= 0;
645 noffset = fdt_next_subnode(fit, noffset)) {
646 const char *node_name;
647 int ret = 0;
648
649 /*
650 * Check subnode name, must be equal to "hash" or "signature".
651 * Multiple hash nodes require unique unit node
b2267e8a 652 * names, e.g. hash-1, hash-2, signature-1, etc.
bbb467dc
SG
653 */
654 node_name = fit_get_name(fit, noffset, NULL);
655 if (!strncmp(node_name, FIT_HASH_NODENAME,
656 strlen(FIT_HASH_NODENAME))) {
657 ret = fit_image_process_hash(fit, image_name, noffset,
658 data, size);
36bfcb62 659 } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) &&
56518e71
SG
660 !strncmp(node_name, FIT_SIG_NODENAME,
661 strlen(FIT_SIG_NODENAME))) {
36bfcb62 662 ret = fit_image_process_sig(keydir, keyfile, keydest,
56518e71 663 fit, image_name, noffset, data, size,
5902a397
JK
664 comment, require_keys, engine_id, cmdname,
665 algo_name);
604f23dd 666 }
9737c2d1 667 if (ret < 0)
1152a05e 668 return ret;
bbb467dc
SG
669 }
670
671 return 0;
672}
673
4d098529
SG
674struct strlist {
675 int count;
676 char **strings;
677};
678
679static void strlist_init(struct strlist *list)
680{
681 memset(list, '\0', sizeof(*list));
682}
683
684static void strlist_free(struct strlist *list)
685{
686 int i;
687
688 for (i = 0; i < list->count; i++)
689 free(list->strings[i]);
690 free(list->strings);
691}
692
693static int strlist_add(struct strlist *list, const char *str)
694{
695 char *dup;
696
697 dup = strdup(str);
698 list->strings = realloc(list->strings,
699 (list->count + 1) * sizeof(char *));
700 if (!list || !str)
701 return -1;
702 list->strings[list->count++] = dup;
703
704 return 0;
705}
706
70e6bcc4
SG
707static const char *fit_config_get_image_list(const void *fit, int noffset,
708 int *lenp, int *allow_missingp)
4d098529
SG
709{
710 static const char default_list[] = FIT_KERNEL_PROP "\0"
711 FIT_FDT_PROP;
712 const char *prop;
713
70e6bcc4 714 /* If there is an "sign-image" property, use that */
4d098529
SG
715 prop = fdt_getprop(fit, noffset, "sign-images", lenp);
716 if (prop) {
717 *allow_missingp = 0;
718 return *lenp ? prop : NULL;
719 }
720
721 /* Default image list */
722 *allow_missingp = 1;
723 *lenp = sizeof(default_list);
724
725 return default_list;
726}
727
70e6bcc4
SG
728/**
729 * fit_config_add_hash() - Add a list of nodes to hash for an image
730 *
731 * This adds a list of paths to image nodes (as referred to by a particular
732 * offset) that need to be hashed, to protect a configuration
733 *
734 * @fit: Pointer to the FIT format image header
735 * @image_noffset: Offset of image to process (e.g. /images/kernel-1)
736 * @node_inc: List of nodes to add to
737 * @conf_name Configuration-node name, child of /configurations node (only
738 * used for error messages)
739 * @sig_name Signature-node name (only used for error messages)
740 * @iname: Name of image being processed (e.g. "kernel-1" (only used
741 * for error messages)
742 */
743static int fit_config_add_hash(const void *fit, int image_noffset,
744 struct strlist *node_inc, const char *conf_name,
745 const char *sig_name, const char *iname)
5a4116f1 746{
48422343 747 char path[200];
5a4116f1
PR
748 int noffset;
749 int hash_count;
750 int ret;
751
752 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
753 if (ret < 0)
754 goto err_path;
755 if (strlist_add(node_inc, path))
756 goto err_mem;
757
5a4116f1
PR
758 /* Add all this image's hashes */
759 hash_count = 0;
760 for (noffset = fdt_first_subnode(fit, image_noffset);
761 noffset >= 0;
762 noffset = fdt_next_subnode(fit, noffset)) {
763 const char *name = fit_get_name(fit, noffset, NULL);
764
765 if (strncmp(name, FIT_HASH_NODENAME,
766 strlen(FIT_HASH_NODENAME)))
767 continue;
768 ret = fdt_get_path(fit, noffset, path, sizeof(path));
769 if (ret < 0)
770 goto err_path;
771 if (strlist_add(node_inc, path))
772 goto err_mem;
773 hash_count++;
774 }
775
776 if (!hash_count) {
8d8851e8
OS
777 fprintf(stderr,
778 "Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
779 conf_name, sig_name, iname);
5a4116f1
PR
780 return -ENOMSG;
781 }
782
783 /* Add this image's cipher node if present */
784 noffset = fdt_subnode_offset(fit, image_noffset,
785 FIT_CIPHER_NODENAME);
786 if (noffset != -FDT_ERR_NOTFOUND) {
787 if (noffset < 0) {
8d8851e8
OS
788 fprintf(stderr,
789 "Failed to get cipher node in configuration '%s/%s' image '%s': %s\n",
790 conf_name, sig_name, iname,
791 fdt_strerror(noffset));
5a4116f1
PR
792 return -EIO;
793 }
794 ret = fdt_get_path(fit, noffset, path, sizeof(path));
795 if (ret < 0)
796 goto err_path;
797 if (strlist_add(node_inc, path))
798 goto err_mem;
799 }
800
801 return 0;
802
803err_mem:
8d8851e8
OS
804 fprintf(stderr, "Out of memory processing configuration '%s/%s'\n", conf_name,
805 sig_name);
5a4116f1
PR
806 return -ENOMEM;
807
808err_path:
8d8851e8
OS
809 fprintf(stderr, "Failed to get path for image '%s' in configuration '%s/%s': %s\n",
810 iname, conf_name, sig_name, fdt_strerror(ret));
5a4116f1
PR
811 return -ENOENT;
812}
813
70e6bcc4
SG
814/**
815 * fit_config_get_hash_list() - Get the regions to sign
816 *
817 * This calculates a list of nodes to hash for this particular configuration,
818 * returning it as a string list (struct strlist, not a devicetree string list)
819 *
820 * @fit: Pointer to the FIT format image header
821 * @conf_noffset: Offset of configuration node to sign (child of
822 * /configurations node)
823 * @sig_offset: Offset of signature node containing info about how to sign it
824 * (child of 'signatures' node)
825 * @return 0 if OK, -ENOENT if an image referred to by the configuration cannot
826 * be found, -ENOMSG if ther were no images in the configuration
827 */
828static int fit_config_get_hash_list(const void *fit, int conf_noffset,
4d098529
SG
829 int sig_offset, struct strlist *node_inc)
830{
831 int allow_missing;
832 const char *prop, *iname, *end;
833 const char *conf_name, *sig_name;
5a4116f1 834 char name[200];
4d098529
SG
835 int image_count;
836 int ret, len;
837
838 conf_name = fit_get_name(fit, conf_noffset, NULL);
839 sig_name = fit_get_name(fit, sig_offset, NULL);
840
841 /*
842 * Build a list of nodes we need to hash. We always need the root
843 * node and the configuration.
844 */
845 strlist_init(node_inc);
846 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
847 if (strlist_add(node_inc, "/") ||
848 strlist_add(node_inc, name))
849 goto err_mem;
850
851 /* Get a list of images that we intend to sign */
66b36f83 852 prop = fit_config_get_image_list(fit, sig_offset, &len,
4d098529
SG
853 &allow_missing);
854 if (!prop)
855 return 0;
856
857 /* Locate the images */
858 end = prop + len;
859 image_count = 0;
860 for (iname = prop; iname < end; iname += strlen(iname) + 1) {
4d098529 861 int image_noffset;
edfeba75 862 int index, max_index;
4d098529 863
edfeba75 864 max_index = fdt_stringlist_count(fit, conf_noffset, iname);
4d098529 865
edfeba75
PR
866 for (index = 0; index < max_index; index++) {
867 image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset,
868 iname, index);
4d098529 869
edfeba75 870 if (image_noffset < 0) {
8d8851e8
OS
871 fprintf(stderr,
872 "Failed to find image '%s' in configuration '%s/%s'\n",
873 iname, conf_name, sig_name);
edfeba75
PR
874 if (allow_missing)
875 continue;
ef40129c 876
edfeba75
PR
877 return -ENOENT;
878 }
879
70e6bcc4
SG
880 ret = fit_config_add_hash(fit, image_noffset, node_inc,
881 conf_name, sig_name, iname);
edfeba75
PR
882 if (ret < 0)
883 return ret;
884
885 image_count++;
886 }
4d098529
SG
887 }
888
889 if (!image_count) {
8d8851e8
OS
890 fprintf(stderr, "Failed to find any images for configuration '%s/%s'\n",
891 conf_name, sig_name);
4d098529
SG
892 return -ENOMSG;
893 }
894
895 return 0;
896
897err_mem:
8d8851e8
OS
898 fprintf(stderr, "Out of memory processing configuration '%s/%s'\n", conf_name,
899 sig_name);
4d098529 900 return -ENOMEM;
4d098529
SG
901}
902
70e6bcc4
SG
903/**
904 * fit_config_get_regions() - Get the regions to sign
905 *
906 * This calculates a list of node to hash for this particular configuration,
907 * then finds which regions of the devicetree they correspond to.
908 *
909 * @fit: Pointer to the FIT format image header
910 * @conf_noffset: Offset of configuration node to sign (child of
911 * /configurations node)
912 * @sig_offset: Offset of signature node containing info about how to sign it
913 * (child of 'signatures' node)
914 * @regionp: Returns list of regions that need to be hashed (allocated; must be
915 * freed by the caller)
916 * @region_count: Returns number of regions
917 * @region_propp: Returns string-list property containing the list of nodes
918 * that correspond to the regions. Each entry is a full path to the node.
919 * This is in devicetree format, i.e. a \0 between each string. This is
920 * allocated and must be freed by the caller.
921 * @region_proplen: Returns length of *@@region_propp in bytes
922 * @return 0 if OK, -ENOMEM if out of memory, -EIO if the regions to hash could
923 * not be found, -EINVAL if no registers were found to hash
924 */
925static int fit_config_get_regions(const void *fit, int conf_noffset,
926 int sig_offset, struct image_region **regionp,
927 int *region_countp, char **region_propp,
928 int *region_proplen)
4d098529 929{
0abe3323
SA
930 char * const exc_prop[] = {
931 FIT_DATA_PROP,
932 FIT_DATA_SIZE_PROP,
933 FIT_DATA_POSITION_PROP,
934 FIT_DATA_OFFSET_PROP,
935 };
4d098529
SG
936 struct strlist node_inc;
937 struct image_region *region;
938 struct fdt_region fdt_regions[100];
939 const char *conf_name, *sig_name;
940 char path[200];
941 int count, i;
942 char *region_prop;
943 int ret, len;
944
945 conf_name = fit_get_name(fit, conf_noffset, NULL);
70e6bcc4 946 sig_name = fit_get_name(fit, sig_offset, NULL);
4d098529
SG
947 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
948
949 /* Get a list of nodes we want to hash */
70e6bcc4
SG
950 ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset,
951 &node_inc);
4d098529
SG
952 if (ret)
953 return ret;
954
955 /* Get a list of regions to hash */
956 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
957 exc_prop, ARRAY_SIZE(exc_prop),
958 fdt_regions, ARRAY_SIZE(fdt_regions),
959 path, sizeof(path), 1);
960 if (count < 0) {
8d8851e8
OS
961 fprintf(stderr, "Failed to hash configuration '%s/%s': %s\n", conf_name,
962 sig_name, fdt_strerror(ret));
4d098529
SG
963 return -EIO;
964 }
965 if (count == 0) {
8d8851e8
OS
966 fprintf(stderr, "No data to hash for configuration '%s/%s': %s\n",
967 conf_name, sig_name, fdt_strerror(ret));
4d098529
SG
968 return -EINVAL;
969 }
970
971 /* Build our list of data blocks */
972 region = fit_region_make_list(fit, fdt_regions, count, NULL);
973 if (!region) {
8d8851e8
OS
974 fprintf(stderr, "Out of memory hashing configuration '%s/%s'\n",
975 conf_name, sig_name);
4d098529
SG
976 return -ENOMEM;
977 }
978
979 /* Create a list of all hashed properties */
980 debug("Hash nodes:\n");
981 for (i = len = 0; i < node_inc.count; i++) {
982 debug(" %s\n", node_inc.strings[i]);
983 len += strlen(node_inc.strings[i]) + 1;
984 }
985 region_prop = malloc(len);
986 if (!region_prop) {
8d8851e8
OS
987 fprintf(stderr, "Out of memory setting up regions for configuration '%s/%s'\n",
988 conf_name, sig_name);
4d098529
SG
989 return -ENOMEM;
990 }
991 for (i = len = 0; i < node_inc.count;
992 len += strlen(node_inc.strings[i]) + 1, i++)
993 strcpy(region_prop + len, node_inc.strings[i]);
994 strlist_free(&node_inc);
995
996 *region_countp = count;
997 *regionp = region;
998 *region_propp = region_prop;
999 *region_proplen = len;
1000
1001 return 0;
1002}
1003
9737c2d1
SG
1004/**
1005 * fit_config_process_sig - Process a single subnode of the configurations/ node
1006 *
1007 * Generate a signed hash of the supplied data and store it in the node.
1008 *
1009 * @keydir: Directory containing keys to use for signing
1010 * @keydest: Destination FDT blob to write public keys into (NULL if none)
1011 * @fit: pointer to the FIT format image header
1012 * @conf_name name of config being processed (used to display errors)
1013 * @conf_noffset: Offset of configuration node, e.g. '/configurations/conf-1'
1014 * @noffset: subnode offset, e.g. '/configurations/conf-1/sig-1'
1015 * @comment: Comment to add to signature nodes
1016 * @require_keys: Mark all keys as 'required'
1017 * @engine_id: Engine to use for signing
1018 * @cmdname: Command name used when reporting errors
1019 * @return keydest node if @keydest is non-NULL, else 0 if none; -ve error code
1020 * on failure
1021 */
36bfcb62 1022static int fit_config_process_sig(const char *keydir, const char *keyfile,
70e6bcc4 1023 void *keydest, void *fit, const char *conf_name,
36bfcb62 1024 int conf_noffset, int noffset, const char *comment,
5902a397
JK
1025 int require_keys, const char *engine_id, const char *cmdname,
1026 const char *algo_name)
4d098529
SG
1027{
1028 struct image_sign_info info;
1029 const char *node_name;
1030 struct image_region *region;
1031 char *region_prop;
1032 int region_proplen;
1033 int region_count;
1034 uint8_t *value;
1035 uint value_len;
1036 int ret;
1037
1038 node_name = fit_get_name(fit, noffset, NULL);
70e6bcc4
SG
1039 if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
1040 &region_count, &region_prop,
1041 &region_proplen))
4d098529
SG
1042 return -1;
1043
36bfcb62 1044 if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
5902a397
JK
1045 require_keys ? "conf" : NULL, engine_id,
1046 algo_name))
4d098529
SG
1047 return -1;
1048
83dd98e0
AD
1049 ret = info.crypto->sign(&info, region, region_count, &value,
1050 &value_len);
4d098529
SG
1051 free(region);
1052 if (ret) {
8d8851e8
OS
1053 fprintf(stderr, "Failed to sign '%s' signature node in '%s' conf node\n",
1054 node_name, conf_name);
4d098529
SG
1055
1056 /* We allow keys to be missing */
1057 if (ret == -ENOENT)
1058 return 0;
1059 return -1;
1060 }
1061
a9468115 1062 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
5902a397
JK
1063 region_prop, region_proplen, cmdname,
1064 algo_name);
a9468115
SG
1065 if (ret) {
1066 if (ret == -FDT_ERR_NOSPACE)
1067 return -ENOSPC;
8d8851e8
OS
1068 fprintf(stderr,
1069 "Can't write signature for '%s' signature node in '%s' conf node: %s\n",
1070 node_name, conf_name, fdt_strerror(ret));
4d098529
SG
1071 return -1;
1072 }
1073 free(value);
1074 free(region_prop);
1075
1076 /* Get keyname again, as FDT has changed and invalidated our pointer */
72188f54 1077 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
4d098529
SG
1078
1079 /* Write the public key into the supplied FDT file */
a9468115 1080 if (keydest) {
83dd98e0 1081 ret = info.crypto->add_verify_data(&info, keydest);
c033dc8c 1082 if (ret < 0) {
8d8851e8
OS
1083 fprintf(stderr,
1084 "Failed to add verification data for '%s' signature node in '%s' configuration node\n",
1085 node_name, conf_name);
a9468115 1086 }
9737c2d1 1087 return ret;
4d098529
SG
1088 }
1089
1090 return 0;
1091}
1092
36bfcb62
AG
1093static int fit_config_add_verification_data(const char *keydir,
1094 const char *keyfile, void *keydest, void *fit, int conf_noffset,
1095 const char *comment, int require_keys, const char *engine_id,
2d2384bb
SG
1096 const char *cmdname, const char *algo_name,
1097 struct image_summary *summary)
4d098529
SG
1098{
1099 const char *conf_name;
1100 int noffset;
1101
1102 conf_name = fit_get_name(fit, conf_noffset, NULL);
1103
1104 /* Process all hash subnodes of the configuration node */
1105 for (noffset = fdt_first_subnode(fit, conf_noffset);
1106 noffset >= 0;
1107 noffset = fdt_next_subnode(fit, noffset)) {
1108 const char *node_name;
1109 int ret = 0;
1110
1111 node_name = fit_get_name(fit, noffset, NULL);
1112 if (!strncmp(node_name, FIT_SIG_NODENAME,
1113 strlen(FIT_SIG_NODENAME))) {
36bfcb62 1114 ret = fit_config_process_sig(keydir, keyfile, keydest,
4d098529 1115 fit, conf_name, conf_noffset, noffset, comment,
5902a397 1116 require_keys, engine_id, cmdname, algo_name);
2d2384bb
SG
1117 if (ret < 0)
1118 return ret;
1119
1120 summary->sig_offset = noffset;
1121 fdt_get_path(fit, noffset, summary->sig_path,
1122 sizeof(summary->sig_path));
1123
1124 if (keydest) {
1125 summary->keydest_offset = ret;
1126 fdt_get_path(keydest, ret,
1127 summary->keydest_path,
1128 sizeof(summary->keydest_path));
1129 }
4d098529 1130 }
4d098529
SG
1131 }
1132
1133 return 0;
1134}
1135
03e59826 1136#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
6e052d1c
PR
1137/*
1138 * 0) open file (open)
1139 * 1) read certificate (PEM_read_X509)
1140 * 2) get public key (X509_get_pubkey)
1141 * 3) provide der format (d2i_RSAPublicKey)
1142 */
1143static int read_pub_key(const char *keydir, const void *name,
1144 unsigned char **pubkey, int *pubkey_len)
1145{
1146 char path[1024];
1147 EVP_PKEY *key = NULL;
1148 X509 *cert;
1149 FILE *f;
1150 int ret;
1151
1152 memset(path, 0, 1024);
1153 snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name);
1154
1155 /* Open certificate file */
1156 f = fopen(path, "r");
1157 if (!f) {
1158 fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
1159 path, strerror(errno));
1160 return -EACCES;
1161 }
1162
1163 /* Read the certificate */
1164 cert = NULL;
1165 if (!PEM_read_X509(f, &cert, NULL, NULL)) {
8d8851e8 1166 fprintf(stderr, "Couldn't read certificate");
6e052d1c
PR
1167 ret = -EINVAL;
1168 goto err_cert;
1169 }
1170
1171 /* Get the public key from the certificate. */
1172 key = X509_get_pubkey(cert);
1173 if (!key) {
8d8851e8 1174 fprintf(stderr, "Couldn't read public key\n");
6e052d1c
PR
1175 ret = -EINVAL;
1176 goto err_pubkey;
1177 }
1178
1179 /* Get DER form */
1180 ret = i2d_PublicKey(key, pubkey);
1181 if (ret < 0) {
8d8851e8 1182 fprintf(stderr, "Couldn't get DER form\n");
6e052d1c
PR
1183 ret = -EINVAL;
1184 goto err_pubkey;
1185 }
1186
1187 *pubkey_len = ret;
1188 ret = 0;
1189
1190err_pubkey:
1191 X509_free(cert);
1192err_cert:
1193 fclose(f);
1194 return ret;
1195}
1196
1197int fit_pre_load_data(const char *keydir, void *keydest, void *fit)
1198{
1199 int pre_load_noffset;
1200 const void *algo_name;
1201 const void *key_name;
1202 unsigned char *pubkey = NULL;
1203 int ret, pubkey_len;
1204
1205 if (!keydir || !keydest || !fit)
1206 return 0;
1207
1208 /* Search node pre-load sig */
1209 pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH);
1210 if (pre_load_noffset < 0) {
1211 ret = 0;
1212 goto out;
1213 }
1214
1215 algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL);
1216 key_name = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL);
1217
1218 /* Check that all mandatory properties are present */
1219 if (!algo_name || !key_name) {
1220 if (!algo_name)
8d8851e8
OS
1221 fprintf(stderr, "The property algo-name is missing in the node %s\n",
1222 IMAGE_PRE_LOAD_PATH);
6e052d1c 1223 if (!key_name)
8d8851e8
OS
1224 fprintf(stderr, "The property key-name is missing in the node %s\n",
1225 IMAGE_PRE_LOAD_PATH);
61657180 1226 ret = -EINVAL;
6e052d1c
PR
1227 goto out;
1228 }
1229
1230 /* Read public key */
1231 ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len);
1232 if (ret < 0)
1233 goto out;
1234
1235 /* Add the public key to the device tree */
1236 ret = fdt_setprop(keydest, pre_load_noffset, "public-key",
1237 pubkey, pubkey_len);
1238 if (ret)
8d8851e8
OS
1239 fprintf(stderr, "Can't set public-key in node %s (ret = %d)\n",
1240 IMAGE_PRE_LOAD_PATH, ret);
6e052d1c
PR
1241
1242 out:
1243 return ret;
1244}
03e59826 1245#endif
6e052d1c 1246
7298e422
PR
1247int fit_cipher_data(const char *keydir, void *keydest, void *fit,
1248 const char *comment, int require_keys,
1249 const char *engine_id, const char *cmdname)
1250{
1251 int images_noffset;
1252 int noffset;
1253 int ret;
1254
1255 /* Find images parent node offset */
1256 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1257 if (images_noffset < 0) {
8d8851e8
OS
1258 fprintf(stderr, "Can't find images parent node '%s' (%s)\n",
1259 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
7298e422
PR
1260 return images_noffset;
1261 }
1262
1263 /* Process its subnodes, print out component images details */
1264 for (noffset = fdt_first_subnode(fit, images_noffset);
1265 noffset >= 0;
1266 noffset = fdt_next_subnode(fit, noffset)) {
1267 /*
1268 * Direct child node of the images parent node,
1269 * i.e. component image node.
1270 */
1271 ret = fit_image_cipher_data(keydir, keydest,
1272 fit, noffset, comment,
1273 require_keys, engine_id,
1274 cmdname);
1275 if (ret)
1276 return ret;
1277 }
1278
1279 return 0;
1280}
1281
36bfcb62
AG
1282int fit_add_verification_data(const char *keydir, const char *keyfile,
1283 void *keydest, void *fit, const char *comment,
1284 int require_keys, const char *engine_id,
2d2384bb
SG
1285 const char *cmdname, const char *algo_name,
1286 struct image_summary *summary)
bbb467dc 1287{
4d098529 1288 int images_noffset, confs_noffset;
bbb467dc
SG
1289 int noffset;
1290 int ret;
1291
1292 /* Find images parent node offset */
1293 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1294 if (images_noffset < 0) {
8d8851e8
OS
1295 fprintf(stderr, "Can't find images parent node '%s' (%s)\n",
1296 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
bbb467dc
SG
1297 return images_noffset;
1298 }
1299
1300 /* Process its subnodes, print out component images details */
1301 for (noffset = fdt_first_subnode(fit, images_noffset);
1302 noffset >= 0;
1303 noffset = fdt_next_subnode(fit, noffset)) {
1304 /*
1305 * Direct child node of the images parent node,
1306 * i.e. component image node.
1307 */
36bfcb62 1308 ret = fit_image_add_verification_data(keydir, keyfile, keydest,
795f452e 1309 fit, noffset, comment, require_keys, engine_id,
5902a397 1310 cmdname, algo_name);
90cfae2a 1311 if (ret) {
8d8851e8
OS
1312 fprintf(stderr, "Can't add verification data for node '%s' (%s)\n",
1313 fdt_get_name(fit, noffset, NULL),
1314 fdt_strerror(ret));
bbb467dc 1315 return ret;
90cfae2a 1316 }
604f23dd
SG
1317 }
1318
4d098529 1319 /* If there are no keys, we can't sign configurations */
36bfcb62 1320 if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile))
4d098529
SG
1321 return 0;
1322
1323 /* Find configurations parent node offset */
1324 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1325 if (confs_noffset < 0) {
8d8851e8
OS
1326 fprintf(stderr, "Can't find images parent node '%s' (%s)\n",
1327 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
4d098529
SG
1328 return -ENOENT;
1329 }
1330
1331 /* Process its subnodes, print out component images details */
1332 for (noffset = fdt_first_subnode(fit, confs_noffset);
1333 noffset >= 0;
1334 noffset = fdt_next_subnode(fit, noffset)) {
36bfcb62 1335 ret = fit_config_add_verification_data(keydir, keyfile, keydest,
4d098529 1336 fit, noffset, comment,
f1ca1fde 1337 require_keys,
5902a397 1338 engine_id, cmdname,
2d2384bb 1339 algo_name, summary);
4d098529
SG
1340 if (ret)
1341 return ret;
1342 }
1343
604f23dd
SG
1344 return 0;
1345}
29a23f9d
HS
1346
1347#ifdef CONFIG_FIT_SIGNATURE
c3aa81e3
SG
1348int fit_check_sign(const void *fit, const void *key,
1349 const char *fit_uname_config)
29a23f9d
HS
1350{
1351 int cfg_noffset;
1352 int ret;
1353
c3aa81e3 1354 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
29a23f9d
HS
1355 if (!cfg_noffset)
1356 return -1;
1357
382cf620
SG
1358 printf("Verifying Hash Integrity for node '%s'... ",
1359 fdt_get_name(fit, cfg_noffset, NULL));
ce1400f6
SG
1360 ret = fit_config_verify(fit, cfg_noffset);
1361 if (ret)
1362 return ret;
c3aa81e3 1363 printf("Verified OK, loading images\n");
ce1400f6
SG
1364 ret = bootm_host_load_images(fit, cfg_noffset);
1365
29a23f9d
HS
1366 return ret;
1367}
1368#endif