blob: 90bc9f905f358f5f5064070f28730d58ac68000a [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
Paul-Erwan Riodcfb6332023-12-21 08:26:11 +010017#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
Philippe Reynes3e3899c2022-03-28 22:57:02 +020018#include <openssl/pem.h>
19#include <openssl/evp.h>
Paul-Erwan Riodcfb6332023-12-21 08:26:11 +010020#endif
Philippe Reynes3e3899c2022-03-28 22:57:02 +020021
Simon Glass10a1eca2013-05-07 06:11:54 +000022/**
Simon Glass0ce6e3d2013-05-07 06:11:56 +000023 * 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +030043 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));
Simon Glassec539f72016-07-03 09:40:44 -060046 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
Simon Glass0ce6e3d2013-05-07 06:11:56 +000047 }
48
49 return 0;
50}
51
52/**
Simon Glass25d6e6b2013-05-07 06:11:55 +000053 * 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
Simon Glass6a0efc82021-11-12 12:28:06 -070056 * a hash of the supplied data and store it in the node.
Simon Glass25d6e6b2013-05-07 06:11:55 +000057 *
58 * @fit: pointer to the FIT format image header
Simon Glass6a0efc82021-11-12 12:28:06 -070059 * @image_name: name of image being processed (used to display errors)
Simon Glass25d6e6b2013-05-07 06:11:55 +000060 * @noffset: subnode offset
61 * @data: data to process
62 * @size: size of data in bytes
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010063 * Return: 0 if ok, -1 on error
Simon Glass25d6e6b2013-05-07 06:11:55 +000064 */
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];
Simon Glassee382652013-05-07 06:12:01 +000069 const char *node_name;
Simon Glass25d6e6b2013-05-07 06:11:55 +000070 int value_len;
Jan Kiszka27beec22022-01-14 10:21:17 +010071 const char *algo;
Simon Glassec539f72016-07-03 09:40:44 -060072 int ret;
Simon Glass25d6e6b2013-05-07 06:11:55 +000073
Simon Glassee382652013-05-07 06:12:01 +000074 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass25d6e6b2013-05-07 06:11:55 +000075
76 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +030077 fprintf(stderr,
78 "Can't get hash algo property for '%s' hash node in '%s' image node\n",
79 node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060080 return -ENOENT;
Simon Glass25d6e6b2013-05-07 06:11:55 +000081 }
82
83 if (calculate_hash(data, size, algo, value, &value_len)) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +030084 fprintf(stderr,
85 "Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
86 algo, node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060087 return -EPROTONOSUPPORT;
Simon Glass25d6e6b2013-05-07 06:11:55 +000088 }
89
Simon Glassec539f72016-07-03 09:40:44 -060090 ret = fit_set_hash_value(fit, noffset, value, value_len);
91 if (ret) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +030092 fprintf(stderr, "Can't set hash value for '%s' hash node in '%s' image node\n",
93 node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060094 return ret;
Simon Glass25d6e6b2013-05-07 06:11:55 +000095 }
96
97 return 0;
98}
99
100/**
Simon Glassfbabc0f2013-06-13 15:10:01 -0700101 * fit_image_write_sig() - write the signature to a FIT
Simon Glass10a1eca2013-05-07 06:11:54 +0000102 *
Simon Glassfbabc0f2013-06-13 15:10:01 -0700103 * 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,
Jan Kiszka4043f322022-01-14 10:21:19 +0100117 int region_proplen, const char *cmdname, const char *algo_name)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700118{
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);
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000139 if (!ret) {
140 time_t timestamp = imagetool_get_source_date(cmdname,
141 time(NULL));
Ming Liuf75f1d72021-05-31 09:04:51 +0200142 uint32_t t = cpu_to_uimage(timestamp);
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000143
Ming Liuf75f1d72021-05-31 09:04:51 +0200144 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
145 sizeof(uint32_t));
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000146 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700147 if (region_prop && !ret) {
148 uint32_t strdata[2];
149
150 ret = fdt_setprop(fit, noffset, "hashed-nodes",
151 region_prop, region_proplen);
Teddy Reed9767bf42018-06-09 11:45:20 -0400152 /* This is a legacy offset, it is unused, and must remain 0. */
Simon Glassfbabc0f2013-06-13 15:10:01 -0700153 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 }
Jan Kiszka4043f322022-01-14 10:21:19 +0100160 if (algo_name && !ret)
161 ret = fdt_setprop_string(fit, noffset, "algo", algo_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700162
163 return ret;
164}
165
166static int fit_image_setup_sig(struct image_sign_info *info,
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600167 const char *keydir, const char *keyfile, void *fit,
168 const char *image_name, int noffset, const char *require_keys,
Jan Kiszka4043f322022-01-14 10:21:19 +0100169 const char *engine_id, const char *algo_name)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700170{
171 const char *node_name;
Philippe Reynes12468352018-11-14 13:51:00 +0100172 const char *padding_name;
Simon Glassfbabc0f2013-06-13 15:10:01 -0700173
174 node_name = fit_get_name(fit, noffset, NULL);
Jan Kiszka4043f322022-01-14 10:21:19 +0100175 if (!algo_name) {
176 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300177 fprintf(stderr,
178 "Can't get algo property for '%s' signature node in '%s' image node\n",
179 node_name, image_name);
Jan Kiszka4043f322022-01-14 10:21:19 +0100180 return -1;
181 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700182 }
183
Philippe Reynes12468352018-11-14 13:51:00 +0100184 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
185
Simon Glassfbabc0f2013-06-13 15:10:01 -0700186 memset(info, '\0', sizeof(*info));
187 info->keydir = keydir;
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600188 info->keyfile = keyfile;
Simon Glassd7aabcc2020-03-18 11:44:06 -0600189 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700190 info->fit = fit;
191 info->node_offset = noffset;
Masahiro Yamadac1f137b2017-10-27 13:25:21 +0900192 info->name = strdup(algo_name);
Andrew Duda6616c822016-11-08 18:53:41 +0000193 info->checksum = image_get_checksum_algo(algo_name);
194 info->crypto = image_get_crypto_algo(algo_name);
Philippe Reynes12468352018-11-14 13:51:00 +0100195 info->padding = image_get_padding_algo(padding_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700196 info->require_keys = require_keys;
George McCollister23d14892017-01-06 13:14:17 -0600197 info->engine_id = engine_id;
Andrew Duda6616c822016-11-08 18:53:41 +0000198 if (!info->checksum || !info->crypto) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300199 fprintf(stderr,
200 "Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
201 algo_name, node_name, image_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700202 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
Simon Glass6a0efc82021-11-12 12:28:06 -0700212 * generate a signed hash of the supplied data and store it in the node.
Simon Glassfbabc0f2013-06-13 15:10:01 -0700213 *
214 * @keydir: Directory containing keys to use for signing
Simon Glass6a0efc82021-11-12 12:28:06 -0700215 * @keydest: Destination FDT blob to write public keys into (NULL if none)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700216 * @fit: pointer to the FIT format image header
Simon Glass6a0efc82021-11-12 12:28:06 -0700217 * @image_name: name of image being processed (used to display errors)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700218 * @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'
George McCollister23d14892017-01-06 13:14:17 -0600223 * @engine_id: Engine to use for signing
Simon Glass89c3fb62021-11-12 12:28:12 -0700224 * Return: keydest node if @keydest is non-NULL, else 0 if none; -ve error code
225 * on failure
Simon Glassfbabc0f2013-06-13 15:10:01 -0700226 */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600227static int fit_image_process_sig(const char *keydir, const char *keyfile,
228 void *keydest, void *fit, const char *image_name,
Simon Glassfbabc0f2013-06-13 15:10:01 -0700229 int noffset, const void *data, size_t size,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000230 const char *comment, int require_keys, const char *engine_id,
Jan Kiszka4043f322022-01-14 10:21:19 +0100231 const char *cmdname, const char *algo_name)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700232{
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
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600240 if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
241 noffset, require_keys ? "image" : NULL,
Jan Kiszka4043f322022-01-14 10:21:19 +0100242 engine_id, algo_name))
Simon Glassfbabc0f2013-06-13 15:10:01 -0700243 return -1;
244
245 node_name = fit_get_name(fit, noffset, NULL);
246 region.data = data;
247 region.size = size;
Andrew Duda6616c822016-11-08 18:53:41 +0000248 ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700249 if (ret) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300250 fprintf(stderr, "Failed to sign '%s' signature node in '%s' image node: %d\n",
251 node_name, image_name, ret);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700252
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,
Jan Kiszka4043f322022-01-14 10:21:19 +0100260 NULL, 0, cmdname, algo_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700261 if (ret) {
Simon Glass802aa822014-06-02 22:04:53 -0600262 if (ret == -FDT_ERR_NOSPACE)
263 return -ENOSPC;
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300264 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));
Simon Glassfbabc0f2013-06-13 15:10:01 -0700267 return -1;
268 }
269 free(value);
270
271 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glassd7aabcc2020-03-18 11:44:06 -0600272 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700273
mario.six@gdsys.ccdc4053a2016-07-22 08:58:40 +0200274 /*
275 * Write the public key into the supplied FDT file; this might fail
mario.six@gdsys.ccd019e152016-07-19 11:07:06 +0200276 * several times, since we try signing with successively increasing
mario.six@gdsys.ccdc4053a2016-07-22 08:58:40 +0200277 * size values
278 */
Masahiro Yamada01486762017-10-27 15:04:20 +0900279 if (keydest) {
280 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glass94336dc2021-11-12 12:28:11 -0700281 if (ret < 0) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300282 fprintf(stderr,
283 "Failed to add verification data for '%s' signature node in '%s' image node\n",
284 node_name, image_name);
Masahiro Yamada01486762017-10-27 15:04:20 +0900285 return ret;
286 }
Simon Glass89c3fb62021-11-12 12:28:12 -0700287 /* Return the node that was written to */
288 return ret;
Masahiro Yamada01486762017-10-27 15:04:20 +0900289 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700290
291 return 0;
292}
293
Philippe Reynes3148e422019-12-18 18:25:41 +0100294static 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300304 fprintf(stderr, "Can't open file %s (err=%d => %s)\n",
305 filename, errno, strerror(errno));
Philippe Reynes3148e422019-12-18 18:25:41 +0100306 return -1;
307 }
308
309 /* Compute file size */
310 if (fstat(fd, &sbuf) < 0) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300311 fprintf(stderr, "Can't fstat file %s (err=%d => %s)\n",
312 filename, errno, strerror(errno));
Philippe Reynes3148e422019-12-18 18:25:41 +0100313 goto err;
314 }
315
316 /* Check file size */
317 if (sbuf.st_size != expected_size) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300318 fprintf(stderr, "File %s don't have the expected size (size=%lld, expected=%d)\n",
319 filename, (long long)sbuf.st_size, expected_size);
Philippe Reynes3148e422019-12-18 18:25:41 +0100320 goto err;
321 }
322
323 /* Read data */
324 n = read(fd, data, sbuf.st_size);
325 if (n < 0) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300326 fprintf(stderr, "Can't read file %s (err=%d => %s)\n",
327 filename, errno, strerror(errno));
Philippe Reynes3148e422019-12-18 18:25:41 +0100328 goto err;
329 }
330
331 /* Check that we have read all the file */
332 if (n != sbuf.st_size) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300333 fprintf(stderr, "Can't read all file %s (read %zd bytes, expected %lld)\n",
334 filename, n, (long long)sbuf.st_size);
Philippe Reynes3148e422019-12-18 18:25:41 +0100335 goto err;
336 }
337
338 ret = 0;
339
340err:
341 close(fd);
342 return ret;
343}
344
Philippe Reynes64d67c42020-09-17 15:01:46 +0200345static int get_random_data(void *data, int size)
346{
347 unsigned char *tmp = data;
348 struct timespec date;
Simon Glass7a1745c2021-05-13 19:39:20 -0600349 int i, ret;
Philippe Reynes64d67c42020-09-17 15:01:46 +0200350
351 if (!tmp) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300352 fprintf(stderr, "%s: pointer data is NULL\n", __func__);
Philippe Reynes64d67c42020-09-17 15:01:46 +0200353 ret = -1;
354 goto out;
355 }
356
357 ret = clock_gettime(CLOCK_MONOTONIC, &date);
Simon Glass7a1745c2021-05-13 19:39:20 -0600358 if (ret) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300359 fprintf(stderr, "%s: clock_gettime has failed (%s)\n", __func__,
360 strerror(errno));
Philippe Reynes64d67c42020-09-17 15:01:46 +0200361 goto out;
362 }
363
Philippe Reynes95f2ab22020-11-13 16:37:46 +0100364 srandom(date.tv_nsec);
Philippe Reynes64d67c42020-09-17 15:01:46 +0200365
366 for (i = 0; i < size; i++) {
Philippe Reynes95f2ab22020-11-13 16:37:46 +0100367 *tmp = random() & 0xff;
Philippe Reynes64d67c42020-09-17 15:01:46 +0200368 tmp++;
369 }
370
371 out:
372 return ret;
373}
374
Philippe Reynes3148e422019-12-18 18:25:41 +0100375static int fit_image_setup_cipher(struct image_cipher_info *info,
376 const char *keydir, void *fit,
377 const char *image_name, int image_noffset,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000378 int noffset)
Philippe Reynes3148e422019-12-18 18:25:41 +0100379{
380 char *algo_name;
381 char filename[128];
382 int ret = -1;
383
384 if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300385 fprintf(stderr, "Can't get algo name for cipher in image '%s'\n",
386 image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100387 goto out;
388 }
389
390 info->keydir = keydir;
391
392 /* Read the key name */
Simon Glassd7aabcc2020-03-18 11:44:06 -0600393 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Philippe Reynes3148e422019-12-18 18:25:41 +0100394 if (!info->keyname) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300395 fprintf(stderr, "Can't get key name for cipher in image '%s'\n",
396 image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100397 goto out;
398 }
399
Philippe Reynes64d67c42020-09-17 15:01:46 +0200400 /*
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 */
Philippe Reynes3148e422019-12-18 18:25:41 +0100406 info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
Philippe Reynes3148e422019-12-18 18:25:41 +0100407
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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300414 fprintf(stderr, "Can't get algo for cipher '%s'\n", image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100415 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300423 fprintf(stderr, "Can't allocate memory for key\n");
Philippe Reynes3148e422019-12-18 18:25:41 +0100424 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
Philippe Reynes3148e422019-12-18 18:25:41 +0100432 info->iv = malloc(info->cipher->iv_len);
433 if (!info->iv) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300434 fprintf(stderr, "Can't allocate memory for iv\n");
Philippe Reynes3148e422019-12-18 18:25:41 +0100435 ret = -1;
436 goto out;
437 }
Philippe Reynes64d67c42020-09-17 15:01:46 +0200438
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 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100449
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
Patrick Oppenlanderbe472112020-07-30 14:22:14 +1000460 /* Replace data with ciphered data */
Philippe Reynes3148e422019-12-18 18:25:41 +0100461 ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
462 data_ciphered, data_ciphered_len);
Patrick Oppenlanderbe472112020-07-30 14:22:14 +1000463 if (ret == -FDT_ERR_NOSPACE) {
464 ret = -ENOSPC;
465 goto out;
466 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100467 if (ret) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300468 fprintf(stderr, "Can't replace data with ciphered data (err = %d)\n", ret);
Philippe Reynes3148e422019-12-18 18:25:41 +0100469 goto out;
470 }
471
472 /* add non ciphered data size */
473 ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
Patrick Oppenlanderbe472112020-07-30 14:22:14 +1000474 if (ret == -FDT_ERR_NOSPACE) {
475 ret = -ENOSPC;
476 goto out;
477 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100478 if (ret) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300479 fprintf(stderr, "Can't add unciphered data size (err = %d)\n", ret);
Philippe Reynes3148e422019-12-18 18:25:41 +0100480 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,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000490 int node_noffset, const void *data, size_t size,
Philippe Reynes3148e422019-12-18 18:25:41 +0100491 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,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000501 image_noffset, node_noffset);
Philippe Reynes3148e422019-12-18 18:25:41 +0100502 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
Philippe Reynes64d67c42020-09-17 15:01:46 +0200514 * And, if needed, write the iv in the FIT file
Philippe Reynes3148e422019-12-18 18:25:41 +0100515 */
516 if (keydest) {
Philippe Reynes64d67c42020-09-17 15:01:46 +0200517 ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
Philippe Reynes3148e422019-12-18 18:25:41 +0100518 if (ret) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300519 fprintf(stderr,
520 "Failed to add verification data for cipher '%s' in image '%s'\n",
521 info.keyname, image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100522 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;
Patrick Oppenlandera4fafb32020-07-30 14:22:15 +1000545 int cipher_node_offset, len;
Philippe Reynes3148e422019-12-18 18:25:41 +0100546
547 /* Get image name */
548 image_name = fit_get_name(fit, image_noffset, NULL);
549 if (!image_name) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300550 fprintf(stderr, "Can't get image name\n");
Philippe Reynes3148e422019-12-18 18:25:41 +0100551 return -1;
552 }
553
554 /* Get image data and data length */
555 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300556 fprintf(stderr, "Can't get image data/size\n");
Philippe Reynes3148e422019-12-18 18:25:41 +0100557 return -1;
558 }
559
Patrick Oppenlandera4fafb32020-07-30 14:22:15 +1000560 /*
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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300570 fprintf(stderr, "Failure testing for data-size-unciphered\n");
Patrick Oppenlandera4fafb32020-07-30 14:22:15 +1000571 return -1;
572 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100573
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000574 /* 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300580 fprintf(stderr, "Failure getting cipher node\n");
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000581 return -1;
Philippe Reynes3148e422019-12-18 18:25:41 +0100582 }
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000583 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);
Philippe Reynes3148e422019-12-18 18:25:41 +0100587}
588
Simon Glassfbabc0f2013-06-13 15:10:01 -0700589/**
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.
Simon Glassee382652013-05-07 06:12:01 +0000593 *
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:
Simon Glass10a1eca2013-05-07 06:11:54 +0000597 *
598 * Input component image node structure:
599 *
Andre Przywara3234ecd2017-12-04 02:05:10 +0000600 * o image-1 (at image_noffset)
Simon Glass10a1eca2013-05-07 06:11:54 +0000601 * | - data = [binary data]
Andre Przywara3234ecd2017-12-04 02:05:10 +0000602 * o hash-1
Simon Glass10a1eca2013-05-07 06:11:54 +0000603 * |- algo = "sha1"
604 *
605 * Output component image node structure:
606 *
Andre Przywara3234ecd2017-12-04 02:05:10 +0000607 * o image-1 (at image_noffset)
Simon Glass10a1eca2013-05-07 06:11:54 +0000608 * | - data = [binary data]
Andre Przywara3234ecd2017-12-04 02:05:10 +0000609 * o hash-1
Simon Glass10a1eca2013-05-07 06:11:54 +0000610 * |- algo = "sha1"
611 * |- value = sha1(data)
612 *
Simon Glassee382652013-05-07 06:12:01 +0000613 * For signature details, please see doc/uImage.FIT/signature.txt
614 *
Simon Glassfbabc0f2013-06-13 15:10:01 -0700615 * @keydir Directory containing *.key and *.crt files (or NULL)
616 * @keydest FDT Blob to write public keys into (NULL if none)
Simon Glassee382652013-05-07 06:12:01 +0000617 * @fit: Pointer to the FIT format image header
618 * @image_noffset: Requested component image node
Simon Glassfbabc0f2013-06-13 15:10:01 -0700619 * @comment: Comment to add to signature nodes
620 * @require_keys: Mark all keys as 'required'
George McCollister23d14892017-01-06 13:14:17 -0600621 * @engine_id: Engine to use for signing
Simon Glassee382652013-05-07 06:12:01 +0000622 * @return: 0 on success, <0 on failure
Simon Glass10a1eca2013-05-07 06:11:54 +0000623 */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600624int 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,
Jan Kiszka4043f322022-01-14 10:21:19 +0100627 const char *cmdname, const char* algo_name)
Simon Glass10a1eca2013-05-07 06:11:54 +0000628{
Simon Glassee382652013-05-07 06:12:01 +0000629 const char *image_name;
Simon Glass10a1eca2013-05-07 06:11:54 +0000630 const void *data;
631 size_t size;
Simon Glass10a1eca2013-05-07 06:11:54 +0000632 int noffset;
Simon Glass10a1eca2013-05-07 06:11:54 +0000633
634 /* Get image data and data length */
635 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300636 fprintf(stderr, "Can't get image data/size\n");
Simon Glass10a1eca2013-05-07 06:11:54 +0000637 return -1;
638 }
639
Simon Glass25d6e6b2013-05-07 06:11:55 +0000640 image_name = fit_get_name(fit, image_noffset, NULL);
641
Simon Glass10a1eca2013-05-07 06:11:54 +0000642 /* Process all hash subnodes of the component image node */
Simon Glassee382652013-05-07 06:12:01 +0000643 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
Andre Przywara3234ecd2017-12-04 02:05:10 +0000652 * names, e.g. hash-1, hash-2, signature-1, etc.
Simon Glassee382652013-05-07 06:12:01 +0000653 */
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);
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600659 } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) &&
Simon Glassfbabc0f2013-06-13 15:10:01 -0700660 !strncmp(node_name, FIT_SIG_NODENAME,
661 strlen(FIT_SIG_NODENAME))) {
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600662 ret = fit_image_process_sig(keydir, keyfile, keydest,
Simon Glassfbabc0f2013-06-13 15:10:01 -0700663 fit, image_name, noffset, data, size,
Jan Kiszka4043f322022-01-14 10:21:19 +0100664 comment, require_keys, engine_id, cmdname,
665 algo_name);
Simon Glass10a1eca2013-05-07 06:11:54 +0000666 }
Simon Glass89c3fb62021-11-12 12:28:12 -0700667 if (ret < 0)
Simon Glassec539f72016-07-03 09:40:44 -0600668 return ret;
Simon Glassee382652013-05-07 06:12:01 +0000669 }
670
671 return 0;
672}
673
Simon Glass56ab8d62013-06-13 15:10:09 -0700674struct 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
Simon Glass6a0efc82021-11-12 12:28:06 -0700707static const char *fit_config_get_image_list(const void *fit, int noffset,
708 int *lenp, int *allow_missingp)
Simon Glass56ab8d62013-06-13 15:10:09 -0700709{
710 static const char default_list[] = FIT_KERNEL_PROP "\0"
711 FIT_FDT_PROP;
712 const char *prop;
713
Simon Glass6a0efc82021-11-12 12:28:06 -0700714 /* If there is an "sign-image" property, use that */
Simon Glass56ab8d62013-06-13 15:10:09 -0700715 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
Simon Glass6a0efc82021-11-12 12:28:06 -0700728/**
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)
Philippe Reynes856bcc82020-11-24 14:39:47 +0100746{
Simon Glass888c0c52021-11-12 12:28:07 -0700747 char path[200];
Philippe Reynes856bcc82020-11-24 14:39:47 +0100748 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
Philippe Reynes856bcc82020-11-24 14:39:47 +0100758 /* 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300777 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);
Philippe Reynes856bcc82020-11-24 14:39:47 +0100780 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300788 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));
Philippe Reynes856bcc82020-11-24 14:39:47 +0100792 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:
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300804 fprintf(stderr, "Out of memory processing configuration '%s/%s'\n", conf_name,
805 sig_name);
Philippe Reynes856bcc82020-11-24 14:39:47 +0100806 return -ENOMEM;
807
808err_path:
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300809 fprintf(stderr, "Failed to get path for image '%s' in configuration '%s/%s': %s\n",
810 iname, conf_name, sig_name, fdt_strerror(ret));
Philippe Reynes856bcc82020-11-24 14:39:47 +0100811 return -ENOENT;
812}
813
Simon Glass6a0efc82021-11-12 12:28:06 -0700814/**
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,
Simon Glass56ab8d62013-06-13 15:10:09 -0700829 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;
Philippe Reynes856bcc82020-11-24 14:39:47 +0100834 char name[200];
Simon Glass56ab8d62013-06-13 15:10:09 -0700835 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 */
Heiko Schocherfdfd5202014-03-03 12:19:23 +0100852 prop = fit_config_get_image_list(fit, sig_offset, &len,
Simon Glass56ab8d62013-06-13 15:10:09 -0700853 &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) {
Simon Glass56ab8d62013-06-13 15:10:09 -0700861 int image_noffset;
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100862 int index, max_index;
Simon Glass56ab8d62013-06-13 15:10:09 -0700863
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100864 max_index = fdt_stringlist_count(fit, conf_noffset, iname);
Simon Glass56ab8d62013-06-13 15:10:09 -0700865
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100866 for (index = 0; index < max_index; index++) {
867 image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset,
868 iname, index);
869
870 if (image_noffset < 0) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300871 fprintf(stderr,
872 "Failed to find image '%s' in configuration '%s/%s'\n",
873 iname, conf_name, sig_name);
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100874 if (allow_missing)
875 continue;
Simon Glass56ab8d62013-06-13 15:10:09 -0700876
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100877 return -ENOENT;
878 }
Patrick Oppenlander5964c3f2020-07-30 14:30:47 +1000879
Simon Glass6a0efc82021-11-12 12:28:06 -0700880 ret = fit_config_add_hash(fit, image_noffset, node_inc,
881 conf_name, sig_name, iname);
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100882 if (ret < 0)
883 return ret;
884
885 image_count++;
886 }
Simon Glass56ab8d62013-06-13 15:10:09 -0700887 }
888
889 if (!image_count) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300890 fprintf(stderr, "Failed to find any images for configuration '%s/%s'\n",
891 conf_name, sig_name);
Simon Glass56ab8d62013-06-13 15:10:09 -0700892 return -ENOMSG;
893 }
894
895 return 0;
896
897err_mem:
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300898 fprintf(stderr, "Out of memory processing configuration '%s/%s'\n", conf_name,
899 sig_name);
Simon Glass56ab8d62013-06-13 15:10:09 -0700900 return -ENOMEM;
Simon Glass56ab8d62013-06-13 15:10:09 -0700901}
902
Simon Glass6a0efc82021-11-12 12:28:06 -0700903/**
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)
Simon Glass56ab8d62013-06-13 15:10:09 -0700929{
Sean Anderson016f4d52022-10-20 15:41:10 -0400930 char * const exc_prop[] = {
931 FIT_DATA_PROP,
932 FIT_DATA_SIZE_PROP,
933 FIT_DATA_POSITION_PROP,
934 FIT_DATA_OFFSET_PROP,
935 };
Simon Glass56ab8d62013-06-13 15:10:09 -0700936 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);
Simon Glass6a0efc82021-11-12 12:28:06 -0700946 sig_name = fit_get_name(fit, sig_offset, NULL);
Simon Glass56ab8d62013-06-13 15:10:09 -0700947 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
948
949 /* Get a list of nodes we want to hash */
Simon Glass6a0efc82021-11-12 12:28:06 -0700950 ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset,
951 &node_inc);
Simon Glass56ab8d62013-06-13 15:10:09 -0700952 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300961 fprintf(stderr, "Failed to hash configuration '%s/%s': %s\n", conf_name,
962 sig_name, fdt_strerror(ret));
Simon Glass56ab8d62013-06-13 15:10:09 -0700963 return -EIO;
964 }
965 if (count == 0) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300966 fprintf(stderr, "No data to hash for configuration '%s/%s': %s\n",
967 conf_name, sig_name, fdt_strerror(ret));
Simon Glass56ab8d62013-06-13 15:10:09 -0700968 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300974 fprintf(stderr, "Out of memory hashing configuration '%s/%s'\n",
975 conf_name, sig_name);
Simon Glass56ab8d62013-06-13 15:10:09 -0700976 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +0300987 fprintf(stderr, "Out of memory setting up regions for configuration '%s/%s'\n",
988 conf_name, sig_name);
Simon Glass56ab8d62013-06-13 15:10:09 -0700989 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
Simon Glass89c3fb62021-11-12 12:28:12 -07001004/**
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 */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001022static int fit_config_process_sig(const char *keydir, const char *keyfile,
Simon Glass6a0efc82021-11-12 12:28:06 -07001023 void *keydest, void *fit, const char *conf_name,
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001024 int conf_noffset, int noffset, const char *comment,
Jan Kiszka4043f322022-01-14 10:21:19 +01001025 int require_keys, const char *engine_id, const char *cmdname,
1026 const char *algo_name)
Simon Glass56ab8d62013-06-13 15:10:09 -07001027{
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);
Simon Glass6a0efc82021-11-12 12:28:06 -07001039 if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
1040 &region_count, &region_prop,
1041 &region_proplen))
Simon Glass56ab8d62013-06-13 15:10:09 -07001042 return -1;
1043
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001044 if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
Jan Kiszka4043f322022-01-14 10:21:19 +01001045 require_keys ? "conf" : NULL, engine_id,
1046 algo_name))
Simon Glass56ab8d62013-06-13 15:10:09 -07001047 return -1;
1048
Andrew Duda6616c822016-11-08 18:53:41 +00001049 ret = info.crypto->sign(&info, region, region_count, &value,
1050 &value_len);
Simon Glass56ab8d62013-06-13 15:10:09 -07001051 free(region);
1052 if (ret) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001053 fprintf(stderr, "Failed to sign '%s' signature node in '%s' conf node\n",
1054 node_name, conf_name);
Simon Glass56ab8d62013-06-13 15:10:09 -07001055
1056 /* We allow keys to be missing */
1057 if (ret == -ENOENT)
1058 return 0;
1059 return -1;
1060 }
1061
Simon Glass802aa822014-06-02 22:04:53 -06001062 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Jan Kiszka4043f322022-01-14 10:21:19 +01001063 region_prop, region_proplen, cmdname,
1064 algo_name);
Simon Glass802aa822014-06-02 22:04:53 -06001065 if (ret) {
1066 if (ret == -FDT_ERR_NOSPACE)
1067 return -ENOSPC;
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001068 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));
Simon Glass56ab8d62013-06-13 15:10:09 -07001071 return -1;
1072 }
1073 free(value);
1074 free(region_prop);
1075
1076 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glassd7aabcc2020-03-18 11:44:06 -06001077 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56ab8d62013-06-13 15:10:09 -07001078
1079 /* Write the public key into the supplied FDT file */
Simon Glass802aa822014-06-02 22:04:53 -06001080 if (keydest) {
Andrew Duda6616c822016-11-08 18:53:41 +00001081 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glass94336dc2021-11-12 12:28:11 -07001082 if (ret < 0) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001083 fprintf(stderr,
1084 "Failed to add verification data for '%s' signature node in '%s' configuration node\n",
1085 node_name, conf_name);
Simon Glass802aa822014-06-02 22:04:53 -06001086 }
Simon Glass89c3fb62021-11-12 12:28:12 -07001087 return ret;
Simon Glass56ab8d62013-06-13 15:10:09 -07001088 }
1089
1090 return 0;
1091}
1092
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001093static 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,
Simon Glasse4607262021-11-12 12:28:13 -07001096 const char *cmdname, const char *algo_name,
1097 struct image_summary *summary)
Simon Glass56ab8d62013-06-13 15:10:09 -07001098{
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))) {
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001114 ret = fit_config_process_sig(keydir, keyfile, keydest,
Simon Glass56ab8d62013-06-13 15:10:09 -07001115 fit, conf_name, conf_noffset, noffset, comment,
Jan Kiszka4043f322022-01-14 10:21:19 +01001116 require_keys, engine_id, cmdname, algo_name);
Simon Glasse4607262021-11-12 12:28:13 -07001117 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 }
Simon Glass56ab8d62013-06-13 15:10:09 -07001130 }
Simon Glass56ab8d62013-06-13 15:10:09 -07001131 }
1132
1133 return 0;
1134}
1135
Paul-Erwan Riodcfb6332023-12-21 08:26:11 +01001136#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001137/*
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)) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001166 fprintf(stderr, "Couldn't read certificate");
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001167 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001174 fprintf(stderr, "Couldn't read public key\n");
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001175 ret = -EINVAL;
1176 goto err_pubkey;
1177 }
1178
1179 /* Get DER form */
1180 ret = i2d_PublicKey(key, pubkey);
1181 if (ret < 0) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001182 fprintf(stderr, "Couldn't get DER form\n");
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001183 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)
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001221 fprintf(stderr, "The property algo-name is missing in the node %s\n",
1222 IMAGE_PRE_LOAD_PATH);
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001223 if (!key_name)
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001224 fprintf(stderr, "The property key-name is missing in the node %s\n",
1225 IMAGE_PRE_LOAD_PATH);
Mark Kettenisd4ba0b42022-04-26 19:24:38 +02001226 ret = -EINVAL;
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001227 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)
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001239 fprintf(stderr, "Can't set public-key in node %s (ret = %d)\n",
1240 IMAGE_PRE_LOAD_PATH, ret);
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001241
1242 out:
1243 return ret;
1244}
Paul-Erwan Riodcfb6332023-12-21 08:26:11 +01001245#endif
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001246
Philippe Reynes3148e422019-12-18 18:25:41 +01001247int 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001258 fprintf(stderr, "Can't find images parent node '%s' (%s)\n",
1259 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Philippe Reynes3148e422019-12-18 18:25:41 +01001260 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
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001282int 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,
Simon Glasse4607262021-11-12 12:28:13 -07001285 const char *cmdname, const char *algo_name,
1286 struct image_summary *summary)
Simon Glassee382652013-05-07 06:12:01 +00001287{
Simon Glass56ab8d62013-06-13 15:10:09 -07001288 int images_noffset, confs_noffset;
Simon Glassee382652013-05-07 06:12:01 +00001289 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) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001295 fprintf(stderr, "Can't find images parent node '%s' (%s)\n",
1296 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Simon Glassee382652013-05-07 06:12:01 +00001297 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 */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001308 ret = fit_image_add_verification_data(keydir, keyfile, keydest,
Alex Kiernan697fcdc2018-06-20 20:10:52 +00001309 fit, noffset, comment, require_keys, engine_id,
Jan Kiszka4043f322022-01-14 10:21:19 +01001310 cmdname, algo_name);
Simon Glass35414072022-12-21 16:08:23 -07001311 if (ret) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001312 fprintf(stderr, "Can't add verification data for node '%s' (%s)\n",
1313 fdt_get_name(fit, noffset, NULL),
1314 fdt_strerror(ret));
Simon Glassee382652013-05-07 06:12:01 +00001315 return ret;
Simon Glass35414072022-12-21 16:08:23 -07001316 }
Simon Glass10a1eca2013-05-07 06:11:54 +00001317 }
1318
Simon Glass56ab8d62013-06-13 15:10:09 -07001319 /* If there are no keys, we can't sign configurations */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001320 if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile))
Simon Glass56ab8d62013-06-13 15:10:09 -07001321 return 0;
1322
1323 /* Find configurations parent node offset */
1324 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1325 if (confs_noffset < 0) {
Oleksandr Suvorov9ed732d2023-08-17 18:36:10 +03001326 fprintf(stderr, "Can't find images parent node '%s' (%s)\n",
1327 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Simon Glass56ab8d62013-06-13 15:10:09 -07001328 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)) {
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001335 ret = fit_config_add_verification_data(keydir, keyfile, keydest,
Simon Glass56ab8d62013-06-13 15:10:09 -07001336 fit, noffset, comment,
George McCollister23d14892017-01-06 13:14:17 -06001337 require_keys,
Jan Kiszka4043f322022-01-14 10:21:19 +01001338 engine_id, cmdname,
Simon Glasse4607262021-11-12 12:28:13 -07001339 algo_name, summary);
Simon Glass56ab8d62013-06-13 15:10:09 -07001340 if (ret)
1341 return ret;
1342 }
1343
Simon Glass10a1eca2013-05-07 06:11:54 +00001344 return 0;
1345}
Heiko Schocherd7b42322014-03-03 12:19:30 +01001346
1347#ifdef CONFIG_FIT_SIGNATURE
Simon Glass05712322020-03-18 11:44:03 -06001348int fit_check_sign(const void *fit, const void *key,
1349 const char *fit_uname_config)
Heiko Schocherd7b42322014-03-03 12:19:30 +01001350{
1351 int cfg_noffset;
1352 int ret;
1353
Simon Glass05712322020-03-18 11:44:03 -06001354 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
Heiko Schocherd7b42322014-03-03 12:19:30 +01001355 if (!cfg_noffset)
1356 return -1;
1357
Simon Glassa559bb22020-03-18 11:43:56 -06001358 printf("Verifying Hash Integrity for node '%s'... ",
1359 fdt_get_name(fit, cfg_noffset, NULL));
Simon Glassa51991d2014-06-12 07:24:53 -06001360 ret = fit_config_verify(fit, cfg_noffset);
1361 if (ret)
1362 return ret;
Simon Glass05712322020-03-18 11:44:03 -06001363 printf("Verified OK, loading images\n");
Simon Glassa51991d2014-06-12 07:24:53 -06001364 ret = bootm_host_load_images(fit, cfg_noffset);
1365
Heiko Schocherd7b42322014-03-03 12:19:30 +01001366 return ret;
1367}
1368#endif