blob: dd7ecc4b609b1b6c28d2dd848a46bcb1c91d7e1c [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>
Masahiro Yamada6dd10522020-04-16 18:30:18 +090013#include <fdt_region.h>
Simon Glass10a1eca2013-05-07 06:11:54 +000014#include <image.h>
Simon Glassfbabc0f2013-06-13 15:10:01 -070015#include <version.h>
Simon Glass10a1eca2013-05-07 06:11:54 +000016
17/**
Simon Glass0ce6e3d2013-05-07 06:11:56 +000018 * fit_set_hash_value - set hash value in requested has node
19 * @fit: pointer to the FIT format image header
20 * @noffset: hash node offset
21 * @value: hash value to be set
22 * @value_len: hash value length
23 *
24 * fit_set_hash_value() attempts to set hash value in a node at offset
25 * given and returns operation status to the caller.
26 *
27 * returns
28 * 0, on success
29 * -1, on failure
30 */
31static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
32 int value_len)
33{
34 int ret;
35
36 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
37 if (ret) {
38 printf("Can't set hash '%s' property for '%s' node(%s)\n",
39 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
40 fdt_strerror(ret));
Simon Glassec539f72016-07-03 09:40:44 -060041 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
Simon Glass0ce6e3d2013-05-07 06:11:56 +000042 }
43
44 return 0;
45}
46
47/**
Simon Glass25d6e6b2013-05-07 06:11:55 +000048 * fit_image_process_hash - Process a single subnode of the images/ node
49 *
50 * Check each subnode and process accordingly. For hash nodes we generate
51 * a hash of the supplised data and store it in the node.
52 *
53 * @fit: pointer to the FIT format image header
54 * @image_name: name of image being processes (used to display errors)
55 * @noffset: subnode offset
56 * @data: data to process
57 * @size: size of data in bytes
58 * @return 0 if ok, -1 on error
59 */
60static int fit_image_process_hash(void *fit, const char *image_name,
61 int noffset, const void *data, size_t size)
62{
63 uint8_t value[FIT_MAX_HASH_LEN];
Simon Glassee382652013-05-07 06:12:01 +000064 const char *node_name;
Simon Glass25d6e6b2013-05-07 06:11:55 +000065 int value_len;
66 char *algo;
Simon Glassec539f72016-07-03 09:40:44 -060067 int ret;
Simon Glass25d6e6b2013-05-07 06:11:55 +000068
Simon Glassee382652013-05-07 06:12:01 +000069 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass25d6e6b2013-05-07 06:11:55 +000070
71 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
72 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000073 node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060074 return -ENOENT;
Simon Glass25d6e6b2013-05-07 06:11:55 +000075 }
76
77 if (calculate_hash(data, size, algo, value, &value_len)) {
78 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000079 algo, node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060080 return -EPROTONOSUPPORT;
Simon Glass25d6e6b2013-05-07 06:11:55 +000081 }
82
Simon Glassec539f72016-07-03 09:40:44 -060083 ret = fit_set_hash_value(fit, noffset, value, value_len);
84 if (ret) {
Simon Glass25d6e6b2013-05-07 06:11:55 +000085 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000086 node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060087 return ret;
Simon Glass25d6e6b2013-05-07 06:11:55 +000088 }
89
90 return 0;
91}
92
93/**
Simon Glassfbabc0f2013-06-13 15:10:01 -070094 * fit_image_write_sig() - write the signature to a FIT
Simon Glass10a1eca2013-05-07 06:11:54 +000095 *
Simon Glassfbabc0f2013-06-13 15:10:01 -070096 * This writes the signature and signer data to the FIT.
97 *
98 * @fit: pointer to the FIT format image header
99 * @noffset: hash node offset
100 * @value: signature value to be set
101 * @value_len: signature value length
102 * @comment: Text comment to write (NULL for none)
103 *
104 * returns
105 * 0, on success
106 * -FDT_ERR_..., on failure
107 */
108static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
109 int value_len, const char *comment, const char *region_prop,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000110 int region_proplen, const char *cmdname)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700111{
112 int string_size;
113 int ret;
114
115 /*
116 * Get the current string size, before we update the FIT and add
117 * more
118 */
119 string_size = fdt_size_dt_strings(fit);
120
121 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
122 if (!ret) {
123 ret = fdt_setprop_string(fit, noffset, "signer-name",
124 "mkimage");
125 }
126 if (!ret) {
127 ret = fdt_setprop_string(fit, noffset, "signer-version",
128 PLAIN_VERSION);
129 }
130 if (comment && !ret)
131 ret = fdt_setprop_string(fit, noffset, "comment", comment);
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000132 if (!ret) {
133 time_t timestamp = imagetool_get_source_date(cmdname,
134 time(NULL));
135
136 ret = fit_set_timestamp(fit, noffset, timestamp);
137 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700138 if (region_prop && !ret) {
139 uint32_t strdata[2];
140
141 ret = fdt_setprop(fit, noffset, "hashed-nodes",
142 region_prop, region_proplen);
Teddy Reed9767bf42018-06-09 11:45:20 -0400143 /* This is a legacy offset, it is unused, and must remain 0. */
Simon Glassfbabc0f2013-06-13 15:10:01 -0700144 strdata[0] = 0;
145 strdata[1] = cpu_to_fdt32(string_size);
146 if (!ret) {
147 ret = fdt_setprop(fit, noffset, "hashed-strings",
148 strdata, sizeof(strdata));
149 }
150 }
151
152 return ret;
153}
154
155static int fit_image_setup_sig(struct image_sign_info *info,
156 const char *keydir, void *fit, const char *image_name,
George McCollister23d14892017-01-06 13:14:17 -0600157 int noffset, const char *require_keys, const char *engine_id)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700158{
159 const char *node_name;
160 char *algo_name;
Philippe Reynes12468352018-11-14 13:51:00 +0100161 const char *padding_name;
Simon Glassfbabc0f2013-06-13 15:10:01 -0700162
163 node_name = fit_get_name(fit, noffset, NULL);
164 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
165 printf("Can't get algo property for '%s' signature node in '%s' image node\n",
166 node_name, image_name);
167 return -1;
168 }
169
Philippe Reynes12468352018-11-14 13:51:00 +0100170 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
171
Simon Glassfbabc0f2013-06-13 15:10:01 -0700172 memset(info, '\0', sizeof(*info));
173 info->keydir = keydir;
Simon Glassd7aabcc2020-03-18 11:44:06 -0600174 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700175 info->fit = fit;
176 info->node_offset = noffset;
Masahiro Yamadac1f137b2017-10-27 13:25:21 +0900177 info->name = strdup(algo_name);
Andrew Duda6616c822016-11-08 18:53:41 +0000178 info->checksum = image_get_checksum_algo(algo_name);
179 info->crypto = image_get_crypto_algo(algo_name);
Philippe Reynes12468352018-11-14 13:51:00 +0100180 info->padding = image_get_padding_algo(padding_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700181 info->require_keys = require_keys;
George McCollister23d14892017-01-06 13:14:17 -0600182 info->engine_id = engine_id;
Andrew Duda6616c822016-11-08 18:53:41 +0000183 if (!info->checksum || !info->crypto) {
Simon Glassfbabc0f2013-06-13 15:10:01 -0700184 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
185 algo_name, node_name, image_name);
186 return -1;
187 }
188
189 return 0;
190}
191
192/**
193 * fit_image_process_sig- Process a single subnode of the images/ node
194 *
195 * Check each subnode and process accordingly. For signature nodes we
196 * generate a signed hash of the supplised data and store it in the node.
197 *
198 * @keydir: Directory containing keys to use for signing
199 * @keydest: Destination FDT blob to write public keys into
200 * @fit: pointer to the FIT format image header
201 * @image_name: name of image being processes (used to display errors)
202 * @noffset: subnode offset
203 * @data: data to process
204 * @size: size of data in bytes
205 * @comment: Comment to add to signature nodes
206 * @require_keys: Mark all keys as 'required'
George McCollister23d14892017-01-06 13:14:17 -0600207 * @engine_id: Engine to use for signing
Simon Glassfbabc0f2013-06-13 15:10:01 -0700208 * @return 0 if ok, -1 on error
209 */
210static int fit_image_process_sig(const char *keydir, void *keydest,
211 void *fit, const char *image_name,
212 int noffset, const void *data, size_t size,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000213 const char *comment, int require_keys, const char *engine_id,
214 const char *cmdname)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700215{
216 struct image_sign_info info;
217 struct image_region region;
218 const char *node_name;
219 uint8_t *value;
220 uint value_len;
221 int ret;
222
223 if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
George McCollister23d14892017-01-06 13:14:17 -0600224 require_keys ? "image" : NULL, engine_id))
Simon Glassfbabc0f2013-06-13 15:10:01 -0700225 return -1;
226
227 node_name = fit_get_name(fit, noffset, NULL);
228 region.data = data;
229 region.size = size;
Andrew Duda6616c822016-11-08 18:53:41 +0000230 ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700231 if (ret) {
232 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
233 node_name, image_name, ret);
234
235 /* We allow keys to be missing */
236 if (ret == -ENOENT)
237 return 0;
238 return -1;
239 }
240
241 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000242 NULL, 0, cmdname);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700243 if (ret) {
Simon Glass802aa822014-06-02 22:04:53 -0600244 if (ret == -FDT_ERR_NOSPACE)
245 return -ENOSPC;
246 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
Simon Glassfbabc0f2013-06-13 15:10:01 -0700247 node_name, image_name, fdt_strerror(ret));
248 return -1;
249 }
250 free(value);
251
252 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glassd7aabcc2020-03-18 11:44:06 -0600253 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700254
mario.six@gdsys.ccdc4053a2016-07-22 08:58:40 +0200255 /*
256 * Write the public key into the supplied FDT file; this might fail
mario.six@gdsys.ccd019e152016-07-19 11:07:06 +0200257 * several times, since we try signing with successively increasing
mario.six@gdsys.ccdc4053a2016-07-22 08:58:40 +0200258 * size values
259 */
Masahiro Yamada01486762017-10-27 15:04:20 +0900260 if (keydest) {
261 ret = info.crypto->add_verify_data(&info, keydest);
262 if (ret) {
263 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
264 node_name, image_name);
265 return ret;
266 }
267 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700268
269 return 0;
270}
271
Philippe Reynes3148e422019-12-18 18:25:41 +0100272static int fit_image_read_data(char *filename, unsigned char *data,
273 int expected_size)
274{
275 struct stat sbuf;
276 int fd, ret = -1;
277 ssize_t n;
278
279 /* Open file */
280 fd = open(filename, O_RDONLY | O_BINARY);
281 if (fd < 0) {
282 printf("Can't open file %s (err=%d => %s)\n",
283 filename, errno, strerror(errno));
284 return -1;
285 }
286
287 /* Compute file size */
288 if (fstat(fd, &sbuf) < 0) {
289 printf("Can't fstat file %s (err=%d => %s)\n",
290 filename, errno, strerror(errno));
291 goto err;
292 }
293
294 /* Check file size */
295 if (sbuf.st_size != expected_size) {
296 printf("File %s don't have the expected size (size=%ld, expected=%d)\n",
297 filename, sbuf.st_size, expected_size);
298 goto err;
299 }
300
301 /* Read data */
302 n = read(fd, data, sbuf.st_size);
303 if (n < 0) {
304 printf("Can't read file %s (err=%d => %s)\n",
305 filename, errno, strerror(errno));
306 goto err;
307 }
308
309 /* Check that we have read all the file */
310 if (n != sbuf.st_size) {
Heinrich Schuchardt6495fe02020-04-11 18:36:04 +0200311 printf("Can't read all file %s (read %zd bytes, expexted %ld)\n",
Philippe Reynes3148e422019-12-18 18:25:41 +0100312 filename, n, sbuf.st_size);
313 goto err;
314 }
315
316 ret = 0;
317
318err:
319 close(fd);
320 return ret;
321}
322
323static int fit_image_setup_cipher(struct image_cipher_info *info,
324 const char *keydir, void *fit,
325 const char *image_name, int image_noffset,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000326 int noffset)
Philippe Reynes3148e422019-12-18 18:25:41 +0100327{
328 char *algo_name;
329 char filename[128];
330 int ret = -1;
331
332 if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000333 printf("Can't get algo name for cipher in image '%s'\n",
334 image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100335 goto out;
336 }
337
338 info->keydir = keydir;
339
340 /* Read the key name */
Simon Glassd7aabcc2020-03-18 11:44:06 -0600341 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Philippe Reynes3148e422019-12-18 18:25:41 +0100342 if (!info->keyname) {
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000343 printf("Can't get key name for cipher in image '%s'\n",
344 image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100345 goto out;
346 }
347
348 /* Read the IV name */
349 info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
350 if (!info->ivname) {
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000351 printf("Can't get iv name for cipher in image '%s'\n",
352 image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100353 goto out;
354 }
355
356 info->fit = fit;
357 info->node_noffset = noffset;
358 info->name = algo_name;
359
360 info->cipher = image_get_cipher_algo(algo_name);
361 if (!info->cipher) {
362 printf("Can't get algo for cipher '%s'\n", image_name);
363 goto out;
364 }
365
366 /* Read the key in the file */
367 snprintf(filename, sizeof(filename), "%s/%s%s",
368 info->keydir, info->keyname, ".bin");
369 info->key = malloc(info->cipher->key_len);
370 if (!info->key) {
371 printf("Can't allocate memory for key\n");
372 ret = -1;
373 goto out;
374 }
375 ret = fit_image_read_data(filename, (unsigned char *)info->key,
376 info->cipher->key_len);
377 if (ret < 0)
378 goto out;
379
380 /* Read the IV in the file */
381 snprintf(filename, sizeof(filename), "%s/%s%s",
382 info->keydir, info->ivname, ".bin");
383 info->iv = malloc(info->cipher->iv_len);
384 if (!info->iv) {
385 printf("Can't allocate memory for iv\n");
386 ret = -1;
387 goto out;
388 }
389 ret = fit_image_read_data(filename, (unsigned char *)info->iv,
390 info->cipher->iv_len);
391
392 out:
393 return ret;
394}
395
396int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
397 const void *data, size_t size,
398 unsigned char *data_ciphered, int data_ciphered_len)
399{
400 int ret = -1;
401
402 /* Remove unciphered data */
403 ret = fdt_delprop(fit, image_noffset, FIT_DATA_PROP);
404 if (ret) {
405 printf("Can't remove data (err = %d)\n", ret);
406 goto out;
407 }
408
409 /* Add ciphered data */
410 ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
411 data_ciphered, data_ciphered_len);
412 if (ret) {
413 printf("Can't add ciphered data (err = %d)\n", ret);
414 goto out;
415 }
416
417 /* add non ciphered data size */
418 ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
419 if (ret) {
420 printf("Can't add unciphered data size (err = %d)\n", ret);
421 goto out;
422 }
423
424 out:
425 return ret;
426}
427
428static int
429fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
430 const char *image_name, int image_noffset,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000431 int node_noffset, const void *data, size_t size,
Philippe Reynes3148e422019-12-18 18:25:41 +0100432 const char *cmdname)
433{
434 struct image_cipher_info info;
435 unsigned char *data_ciphered = NULL;
436 int data_ciphered_len;
437 int ret;
438
439 memset(&info, 0, sizeof(info));
440
441 ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000442 image_noffset, node_noffset);
Philippe Reynes3148e422019-12-18 18:25:41 +0100443 if (ret)
444 goto out;
445
446 ret = info.cipher->encrypt(&info, data, size,
447 &data_ciphered, &data_ciphered_len);
448 if (ret)
449 goto out;
450
451 /*
452 * Write the public key into the supplied FDT file; this might fail
453 * several times, since we try signing with successively increasing
454 * size values
455 */
456 if (keydest) {
457 ret = info.cipher->add_cipher_data(&info, keydest);
458 if (ret) {
459 printf("Failed to add verification data for cipher '%s' in image '%s'\n",
460 info.keyname, image_name);
461 goto out;
462 }
463 }
464
465 ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
466 data, size,
467 data_ciphered, data_ciphered_len);
468
469 out:
470 free(data_ciphered);
471 free((void *)info.key);
472 free((void *)info.iv);
473 return ret;
474}
475
476int fit_image_cipher_data(const char *keydir, void *keydest,
477 void *fit, int image_noffset, const char *comment,
478 int require_keys, const char *engine_id,
479 const char *cmdname)
480{
481 const char *image_name;
482 const void *data;
483 size_t size;
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000484 int cipher_node_offset;
Philippe Reynes3148e422019-12-18 18:25:41 +0100485
486 /* Get image name */
487 image_name = fit_get_name(fit, image_noffset, NULL);
488 if (!image_name) {
489 printf("Can't get image name\n");
490 return -1;
491 }
492
493 /* Get image data and data length */
494 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
495 printf("Can't get image data/size\n");
496 return -1;
497 }
498
Philippe Reynes3148e422019-12-18 18:25:41 +0100499
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000500 /* Process cipher node if present */
501 cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
502 FIT_CIPHER_NODENAME);
503 if (cipher_node_offset == -FDT_ERR_NOTFOUND)
504 return 0;
505 if (cipher_node_offset < 0) {
506 printf("Failure getting cipher node\n");
507 return -1;
Philippe Reynes3148e422019-12-18 18:25:41 +0100508 }
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000509 if (!IMAGE_ENABLE_ENCRYPT || !keydir)
510 return 0;
511 return fit_image_process_cipher(keydir, keydest, fit, image_name,
512 image_noffset, cipher_node_offset, data, size, cmdname);
Philippe Reynes3148e422019-12-18 18:25:41 +0100513}
514
Simon Glassfbabc0f2013-06-13 15:10:01 -0700515/**
516 * fit_image_add_verification_data() - calculate/set verig. data for image node
517 *
518 * This adds hash and signature values for an component image node.
Simon Glassee382652013-05-07 06:12:01 +0000519 *
520 * All existing hash subnodes are checked, if algorithm property is set to
521 * one of the supported hash algorithms, hash value is computed and
522 * corresponding hash node property is set, for example:
Simon Glass10a1eca2013-05-07 06:11:54 +0000523 *
524 * Input component image node structure:
525 *
Andre Przywara3234ecd2017-12-04 02:05:10 +0000526 * o image-1 (at image_noffset)
Simon Glass10a1eca2013-05-07 06:11:54 +0000527 * | - data = [binary data]
Andre Przywara3234ecd2017-12-04 02:05:10 +0000528 * o hash-1
Simon Glass10a1eca2013-05-07 06:11:54 +0000529 * |- algo = "sha1"
530 *
531 * Output component image node structure:
532 *
Andre Przywara3234ecd2017-12-04 02:05:10 +0000533 * o image-1 (at image_noffset)
Simon Glass10a1eca2013-05-07 06:11:54 +0000534 * | - data = [binary data]
Andre Przywara3234ecd2017-12-04 02:05:10 +0000535 * o hash-1
Simon Glass10a1eca2013-05-07 06:11:54 +0000536 * |- algo = "sha1"
537 * |- value = sha1(data)
538 *
Simon Glassee382652013-05-07 06:12:01 +0000539 * For signature details, please see doc/uImage.FIT/signature.txt
540 *
Simon Glassfbabc0f2013-06-13 15:10:01 -0700541 * @keydir Directory containing *.key and *.crt files (or NULL)
542 * @keydest FDT Blob to write public keys into (NULL if none)
Simon Glassee382652013-05-07 06:12:01 +0000543 * @fit: Pointer to the FIT format image header
544 * @image_noffset: Requested component image node
Simon Glassfbabc0f2013-06-13 15:10:01 -0700545 * @comment: Comment to add to signature nodes
546 * @require_keys: Mark all keys as 'required'
George McCollister23d14892017-01-06 13:14:17 -0600547 * @engine_id: Engine to use for signing
Simon Glassee382652013-05-07 06:12:01 +0000548 * @return: 0 on success, <0 on failure
Simon Glass10a1eca2013-05-07 06:11:54 +0000549 */
Simon Glassfbabc0f2013-06-13 15:10:01 -0700550int fit_image_add_verification_data(const char *keydir, void *keydest,
551 void *fit, int image_noffset, const char *comment,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000552 int require_keys, const char *engine_id, const char *cmdname)
Simon Glass10a1eca2013-05-07 06:11:54 +0000553{
Simon Glassee382652013-05-07 06:12:01 +0000554 const char *image_name;
Simon Glass10a1eca2013-05-07 06:11:54 +0000555 const void *data;
556 size_t size;
Simon Glass10a1eca2013-05-07 06:11:54 +0000557 int noffset;
Simon Glass10a1eca2013-05-07 06:11:54 +0000558
559 /* Get image data and data length */
560 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
561 printf("Can't get image data/size\n");
562 return -1;
563 }
564
Simon Glass25d6e6b2013-05-07 06:11:55 +0000565 image_name = fit_get_name(fit, image_noffset, NULL);
566
Simon Glass10a1eca2013-05-07 06:11:54 +0000567 /* Process all hash subnodes of the component image node */
Simon Glassee382652013-05-07 06:12:01 +0000568 for (noffset = fdt_first_subnode(fit, image_noffset);
569 noffset >= 0;
570 noffset = fdt_next_subnode(fit, noffset)) {
571 const char *node_name;
572 int ret = 0;
573
574 /*
575 * Check subnode name, must be equal to "hash" or "signature".
576 * Multiple hash nodes require unique unit node
Andre Przywara3234ecd2017-12-04 02:05:10 +0000577 * names, e.g. hash-1, hash-2, signature-1, etc.
Simon Glassee382652013-05-07 06:12:01 +0000578 */
579 node_name = fit_get_name(fit, noffset, NULL);
580 if (!strncmp(node_name, FIT_HASH_NODENAME,
581 strlen(FIT_HASH_NODENAME))) {
582 ret = fit_image_process_hash(fit, image_name, noffset,
583 data, size);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700584 } else if (IMAGE_ENABLE_SIGN && keydir &&
585 !strncmp(node_name, FIT_SIG_NODENAME,
586 strlen(FIT_SIG_NODENAME))) {
587 ret = fit_image_process_sig(keydir, keydest,
588 fit, image_name, noffset, data, size,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000589 comment, require_keys, engine_id, cmdname);
Simon Glass10a1eca2013-05-07 06:11:54 +0000590 }
Simon Glassee382652013-05-07 06:12:01 +0000591 if (ret)
Simon Glassec539f72016-07-03 09:40:44 -0600592 return ret;
Simon Glassee382652013-05-07 06:12:01 +0000593 }
594
595 return 0;
596}
597
Simon Glass56ab8d62013-06-13 15:10:09 -0700598struct strlist {
599 int count;
600 char **strings;
601};
602
603static void strlist_init(struct strlist *list)
604{
605 memset(list, '\0', sizeof(*list));
606}
607
608static void strlist_free(struct strlist *list)
609{
610 int i;
611
612 for (i = 0; i < list->count; i++)
613 free(list->strings[i]);
614 free(list->strings);
615}
616
617static int strlist_add(struct strlist *list, const char *str)
618{
619 char *dup;
620
621 dup = strdup(str);
622 list->strings = realloc(list->strings,
623 (list->count + 1) * sizeof(char *));
624 if (!list || !str)
625 return -1;
626 list->strings[list->count++] = dup;
627
628 return 0;
629}
630
631static const char *fit_config_get_image_list(void *fit, int noffset,
632 int *lenp, int *allow_missingp)
633{
634 static const char default_list[] = FIT_KERNEL_PROP "\0"
635 FIT_FDT_PROP;
636 const char *prop;
637
638 /* If there is an "image" property, use that */
639 prop = fdt_getprop(fit, noffset, "sign-images", lenp);
640 if (prop) {
641 *allow_missingp = 0;
642 return *lenp ? prop : NULL;
643 }
644
645 /* Default image list */
646 *allow_missingp = 1;
647 *lenp = sizeof(default_list);
648
649 return default_list;
650}
651
652static int fit_config_get_hash_list(void *fit, int conf_noffset,
653 int sig_offset, struct strlist *node_inc)
654{
655 int allow_missing;
656 const char *prop, *iname, *end;
657 const char *conf_name, *sig_name;
658 char name[200], path[200];
659 int image_count;
660 int ret, len;
661
662 conf_name = fit_get_name(fit, conf_noffset, NULL);
663 sig_name = fit_get_name(fit, sig_offset, NULL);
664
665 /*
666 * Build a list of nodes we need to hash. We always need the root
667 * node and the configuration.
668 */
669 strlist_init(node_inc);
670 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
671 if (strlist_add(node_inc, "/") ||
672 strlist_add(node_inc, name))
673 goto err_mem;
674
675 /* Get a list of images that we intend to sign */
Heiko Schocherfdfd5202014-03-03 12:19:23 +0100676 prop = fit_config_get_image_list(fit, sig_offset, &len,
Simon Glass56ab8d62013-06-13 15:10:09 -0700677 &allow_missing);
678 if (!prop)
679 return 0;
680
681 /* Locate the images */
682 end = prop + len;
683 image_count = 0;
684 for (iname = prop; iname < end; iname += strlen(iname) + 1) {
685 int noffset;
686 int image_noffset;
687 int hash_count;
688
689 image_noffset = fit_conf_get_prop_node(fit, conf_noffset,
690 iname);
691 if (image_noffset < 0) {
692 printf("Failed to find image '%s' in configuration '%s/%s'\n",
693 iname, conf_name, sig_name);
694 if (allow_missing)
695 continue;
696
697 return -ENOENT;
698 }
699
700 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
701 if (ret < 0)
702 goto err_path;
703 if (strlist_add(node_inc, path))
704 goto err_mem;
705
706 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
707 conf_name);
708
709 /* Add all this image's hashes */
710 hash_count = 0;
711 for (noffset = fdt_first_subnode(fit, image_noffset);
712 noffset >= 0;
713 noffset = fdt_next_subnode(fit, noffset)) {
714 const char *name = fit_get_name(fit, noffset, NULL);
715
716 if (strncmp(name, FIT_HASH_NODENAME,
717 strlen(FIT_HASH_NODENAME)))
718 continue;
719 ret = fdt_get_path(fit, noffset, path, sizeof(path));
720 if (ret < 0)
721 goto err_path;
722 if (strlist_add(node_inc, path))
723 goto err_mem;
724 hash_count++;
725 }
726
727 if (!hash_count) {
728 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
729 conf_name, sig_name, iname);
730 return -ENOMSG;
731 }
732
733 image_count++;
734 }
735
736 if (!image_count) {
737 printf("Failed to find any images for configuration '%s/%s'\n",
738 conf_name, sig_name);
739 return -ENOMSG;
740 }
741
742 return 0;
743
744err_mem:
745 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
746 sig_name);
747 return -ENOMEM;
748
749err_path:
750 printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
751 iname, conf_name, sig_name, fdt_strerror(ret));
752 return -ENOENT;
753}
754
755static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
756 struct image_region **regionp, int *region_countp,
757 char **region_propp, int *region_proplen)
758{
759 char * const exc_prop[] = {"data"};
760 struct strlist node_inc;
761 struct image_region *region;
762 struct fdt_region fdt_regions[100];
763 const char *conf_name, *sig_name;
764 char path[200];
765 int count, i;
766 char *region_prop;
767 int ret, len;
768
769 conf_name = fit_get_name(fit, conf_noffset, NULL);
Masahiro Yamada2fc6b6f2017-10-19 19:16:21 +0900770 sig_name = fit_get_name(fit, noffset, NULL);
Simon Glass56ab8d62013-06-13 15:10:09 -0700771 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
772
773 /* Get a list of nodes we want to hash */
774 ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
775 if (ret)
776 return ret;
777
778 /* Get a list of regions to hash */
779 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
780 exc_prop, ARRAY_SIZE(exc_prop),
781 fdt_regions, ARRAY_SIZE(fdt_regions),
782 path, sizeof(path), 1);
783 if (count < 0) {
784 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
785 sig_name, fdt_strerror(ret));
786 return -EIO;
787 }
788 if (count == 0) {
789 printf("No data to hash for configuration '%s/%s': %s\n",
790 conf_name, sig_name, fdt_strerror(ret));
791 return -EINVAL;
792 }
793
794 /* Build our list of data blocks */
795 region = fit_region_make_list(fit, fdt_regions, count, NULL);
796 if (!region) {
797 printf("Out of memory hashing configuration '%s/%s'\n",
798 conf_name, sig_name);
799 return -ENOMEM;
800 }
801
802 /* Create a list of all hashed properties */
803 debug("Hash nodes:\n");
804 for (i = len = 0; i < node_inc.count; i++) {
805 debug(" %s\n", node_inc.strings[i]);
806 len += strlen(node_inc.strings[i]) + 1;
807 }
808 region_prop = malloc(len);
809 if (!region_prop) {
810 printf("Out of memory setting up regions for configuration '%s/%s'\n",
811 conf_name, sig_name);
812 return -ENOMEM;
813 }
814 for (i = len = 0; i < node_inc.count;
815 len += strlen(node_inc.strings[i]) + 1, i++)
816 strcpy(region_prop + len, node_inc.strings[i]);
817 strlist_free(&node_inc);
818
819 *region_countp = count;
820 *regionp = region;
821 *region_propp = region_prop;
822 *region_proplen = len;
823
824 return 0;
825}
826
827static int fit_config_process_sig(const char *keydir, void *keydest,
828 void *fit, const char *conf_name, int conf_noffset,
George McCollister23d14892017-01-06 13:14:17 -0600829 int noffset, const char *comment, int require_keys,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000830 const char *engine_id, const char *cmdname)
Simon Glass56ab8d62013-06-13 15:10:09 -0700831{
832 struct image_sign_info info;
833 const char *node_name;
834 struct image_region *region;
835 char *region_prop;
836 int region_proplen;
837 int region_count;
838 uint8_t *value;
839 uint value_len;
840 int ret;
841
842 node_name = fit_get_name(fit, noffset, NULL);
843 if (fit_config_get_data(fit, conf_noffset, noffset, &region,
844 &region_count, &region_prop, &region_proplen))
845 return -1;
846
847 if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
George McCollister23d14892017-01-06 13:14:17 -0600848 require_keys ? "conf" : NULL, engine_id))
Simon Glass56ab8d62013-06-13 15:10:09 -0700849 return -1;
850
Andrew Duda6616c822016-11-08 18:53:41 +0000851 ret = info.crypto->sign(&info, region, region_count, &value,
852 &value_len);
Simon Glass56ab8d62013-06-13 15:10:09 -0700853 free(region);
854 if (ret) {
855 printf("Failed to sign '%s' signature node in '%s' conf node\n",
856 node_name, conf_name);
857
858 /* We allow keys to be missing */
859 if (ret == -ENOENT)
860 return 0;
861 return -1;
862 }
863
Simon Glass802aa822014-06-02 22:04:53 -0600864 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000865 region_prop, region_proplen, cmdname);
Simon Glass802aa822014-06-02 22:04:53 -0600866 if (ret) {
867 if (ret == -FDT_ERR_NOSPACE)
868 return -ENOSPC;
869 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
870 node_name, conf_name, fdt_strerror(ret));
Simon Glass56ab8d62013-06-13 15:10:09 -0700871 return -1;
872 }
873 free(value);
874 free(region_prop);
875
876 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glassd7aabcc2020-03-18 11:44:06 -0600877 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56ab8d62013-06-13 15:10:09 -0700878
879 /* Write the public key into the supplied FDT file */
Simon Glass802aa822014-06-02 22:04:53 -0600880 if (keydest) {
Andrew Duda6616c822016-11-08 18:53:41 +0000881 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glass802aa822014-06-02 22:04:53 -0600882 if (ret) {
Masahiro Yamadafe391a72017-10-27 15:04:21 +0900883 printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
Simon Glass802aa822014-06-02 22:04:53 -0600884 node_name, conf_name);
Simon Glass802aa822014-06-02 22:04:53 -0600885 }
Simon Glass9423bad2014-06-12 07:24:42 -0600886 return ret;
Simon Glass56ab8d62013-06-13 15:10:09 -0700887 }
888
889 return 0;
890}
891
892static int fit_config_add_verification_data(const char *keydir, void *keydest,
893 void *fit, int conf_noffset, const char *comment,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000894 int require_keys, const char *engine_id, const char *cmdname)
Simon Glass56ab8d62013-06-13 15:10:09 -0700895{
896 const char *conf_name;
897 int noffset;
898
899 conf_name = fit_get_name(fit, conf_noffset, NULL);
900
901 /* Process all hash subnodes of the configuration node */
902 for (noffset = fdt_first_subnode(fit, conf_noffset);
903 noffset >= 0;
904 noffset = fdt_next_subnode(fit, noffset)) {
905 const char *node_name;
906 int ret = 0;
907
908 node_name = fit_get_name(fit, noffset, NULL);
909 if (!strncmp(node_name, FIT_SIG_NODENAME,
910 strlen(FIT_SIG_NODENAME))) {
911 ret = fit_config_process_sig(keydir, keydest,
912 fit, conf_name, conf_noffset, noffset, comment,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000913 require_keys, engine_id, cmdname);
Simon Glass56ab8d62013-06-13 15:10:09 -0700914 }
915 if (ret)
916 return ret;
917 }
918
919 return 0;
920}
921
Philippe Reynes3148e422019-12-18 18:25:41 +0100922int fit_cipher_data(const char *keydir, void *keydest, void *fit,
923 const char *comment, int require_keys,
924 const char *engine_id, const char *cmdname)
925{
926 int images_noffset;
927 int noffset;
928 int ret;
929
930 /* Find images parent node offset */
931 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
932 if (images_noffset < 0) {
933 printf("Can't find images parent node '%s' (%s)\n",
934 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
935 return images_noffset;
936 }
937
938 /* Process its subnodes, print out component images details */
939 for (noffset = fdt_first_subnode(fit, images_noffset);
940 noffset >= 0;
941 noffset = fdt_next_subnode(fit, noffset)) {
942 /*
943 * Direct child node of the images parent node,
944 * i.e. component image node.
945 */
946 ret = fit_image_cipher_data(keydir, keydest,
947 fit, noffset, comment,
948 require_keys, engine_id,
949 cmdname);
950 if (ret)
951 return ret;
952 }
953
954 return 0;
955}
956
Simon Glassfbabc0f2013-06-13 15:10:01 -0700957int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
George McCollister23d14892017-01-06 13:14:17 -0600958 const char *comment, int require_keys,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000959 const char *engine_id, const char *cmdname)
Simon Glassee382652013-05-07 06:12:01 +0000960{
Simon Glass56ab8d62013-06-13 15:10:09 -0700961 int images_noffset, confs_noffset;
Simon Glassee382652013-05-07 06:12:01 +0000962 int noffset;
963 int ret;
964
965 /* Find images parent node offset */
966 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
967 if (images_noffset < 0) {
968 printf("Can't find images parent node '%s' (%s)\n",
969 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
970 return images_noffset;
971 }
972
973 /* Process its subnodes, print out component images details */
974 for (noffset = fdt_first_subnode(fit, images_noffset);
975 noffset >= 0;
976 noffset = fdt_next_subnode(fit, noffset)) {
977 /*
978 * Direct child node of the images parent node,
979 * i.e. component image node.
980 */
Simon Glassfbabc0f2013-06-13 15:10:01 -0700981 ret = fit_image_add_verification_data(keydir, keydest,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000982 fit, noffset, comment, require_keys, engine_id,
983 cmdname);
Simon Glassee382652013-05-07 06:12:01 +0000984 if (ret)
985 return ret;
Simon Glass10a1eca2013-05-07 06:11:54 +0000986 }
987
Simon Glass56ab8d62013-06-13 15:10:09 -0700988 /* If there are no keys, we can't sign configurations */
989 if (!IMAGE_ENABLE_SIGN || !keydir)
990 return 0;
991
992 /* Find configurations parent node offset */
993 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
994 if (confs_noffset < 0) {
995 printf("Can't find images parent node '%s' (%s)\n",
Heiko Schocher441f48d2014-08-11 11:02:17 +0200996 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Simon Glass56ab8d62013-06-13 15:10:09 -0700997 return -ENOENT;
998 }
999
1000 /* Process its subnodes, print out component images details */
1001 for (noffset = fdt_first_subnode(fit, confs_noffset);
1002 noffset >= 0;
1003 noffset = fdt_next_subnode(fit, noffset)) {
1004 ret = fit_config_add_verification_data(keydir, keydest,
1005 fit, noffset, comment,
George McCollister23d14892017-01-06 13:14:17 -06001006 require_keys,
Alex Kiernan697fcdc2018-06-20 20:10:52 +00001007 engine_id, cmdname);
Simon Glass56ab8d62013-06-13 15:10:09 -07001008 if (ret)
1009 return ret;
1010 }
1011
Simon Glass10a1eca2013-05-07 06:11:54 +00001012 return 0;
1013}
Heiko Schocherd7b42322014-03-03 12:19:30 +01001014
1015#ifdef CONFIG_FIT_SIGNATURE
Simon Glass05712322020-03-18 11:44:03 -06001016int fit_check_sign(const void *fit, const void *key,
1017 const char *fit_uname_config)
Heiko Schocherd7b42322014-03-03 12:19:30 +01001018{
1019 int cfg_noffset;
1020 int ret;
1021
Simon Glass05712322020-03-18 11:44:03 -06001022 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
Heiko Schocherd7b42322014-03-03 12:19:30 +01001023 if (!cfg_noffset)
1024 return -1;
1025
Simon Glassa559bb22020-03-18 11:43:56 -06001026 printf("Verifying Hash Integrity for node '%s'... ",
1027 fdt_get_name(fit, cfg_noffset, NULL));
Simon Glassa51991d2014-06-12 07:24:53 -06001028 ret = fit_config_verify(fit, cfg_noffset);
1029 if (ret)
1030 return ret;
Simon Glass05712322020-03-18 11:44:03 -06001031 printf("Verified OK, loading images\n");
Simon Glassa51991d2014-06-12 07:24:53 -06001032 ret = bootm_host_load_images(fit, cfg_noffset);
1033
Heiko Schocherd7b42322014-03-03 12:19:30 +01001034 return ret;
1035}
1036#endif