blob: 0bf18df50e762fda93a6f92fdb4b0a567fc2b19d [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
Philippe Reynes3e3899c2022-03-28 22:57:02 +020017#include <openssl/pem.h>
18#include <openssl/evp.h>
19
Simon Glass10a1eca2013-05-07 06:11:54 +000020/**
Simon Glass0ce6e3d2013-05-07 06:11:56 +000021 * fit_set_hash_value - set hash value in requested has node
22 * @fit: pointer to the FIT format image header
23 * @noffset: hash node offset
24 * @value: hash value to be set
25 * @value_len: hash value length
26 *
27 * fit_set_hash_value() attempts to set hash value in a node at offset
28 * given and returns operation status to the caller.
29 *
30 * returns
31 * 0, on success
32 * -1, on failure
33 */
34static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
35 int value_len)
36{
37 int ret;
38
39 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
40 if (ret) {
41 printf("Can't set hash '%s' property for '%s' node(%s)\n",
42 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
43 fdt_strerror(ret));
Simon Glassec539f72016-07-03 09:40:44 -060044 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
Simon Glass0ce6e3d2013-05-07 06:11:56 +000045 }
46
47 return 0;
48}
49
50/**
Simon Glass25d6e6b2013-05-07 06:11:55 +000051 * fit_image_process_hash - Process a single subnode of the images/ node
52 *
53 * Check each subnode and process accordingly. For hash nodes we generate
Simon Glass6a0efc82021-11-12 12:28:06 -070054 * a hash of the supplied data and store it in the node.
Simon Glass25d6e6b2013-05-07 06:11:55 +000055 *
56 * @fit: pointer to the FIT format image header
Simon Glass6a0efc82021-11-12 12:28:06 -070057 * @image_name: name of image being processed (used to display errors)
Simon Glass25d6e6b2013-05-07 06:11:55 +000058 * @noffset: subnode offset
59 * @data: data to process
60 * @size: size of data in bytes
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010061 * Return: 0 if ok, -1 on error
Simon Glass25d6e6b2013-05-07 06:11:55 +000062 */
63static int fit_image_process_hash(void *fit, const char *image_name,
64 int noffset, const void *data, size_t size)
65{
66 uint8_t value[FIT_MAX_HASH_LEN];
Simon Glassee382652013-05-07 06:12:01 +000067 const char *node_name;
Simon Glass25d6e6b2013-05-07 06:11:55 +000068 int value_len;
Jan Kiszka27beec22022-01-14 10:21:17 +010069 const char *algo;
Simon Glassec539f72016-07-03 09:40:44 -060070 int ret;
Simon Glass25d6e6b2013-05-07 06:11:55 +000071
Simon Glassee382652013-05-07 06:12:01 +000072 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass25d6e6b2013-05-07 06:11:55 +000073
74 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
75 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000076 node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060077 return -ENOENT;
Simon Glass25d6e6b2013-05-07 06:11:55 +000078 }
79
80 if (calculate_hash(data, size, algo, value, &value_len)) {
81 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000082 algo, node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060083 return -EPROTONOSUPPORT;
Simon Glass25d6e6b2013-05-07 06:11:55 +000084 }
85
Simon Glassec539f72016-07-03 09:40:44 -060086 ret = fit_set_hash_value(fit, noffset, value, value_len);
87 if (ret) {
Simon Glass25d6e6b2013-05-07 06:11:55 +000088 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
Simon Glassee382652013-05-07 06:12:01 +000089 node_name, image_name);
Simon Glassec539f72016-07-03 09:40:44 -060090 return ret;
Simon Glass25d6e6b2013-05-07 06:11:55 +000091 }
92
93 return 0;
94}
95
96/**
Simon Glassfbabc0f2013-06-13 15:10:01 -070097 * fit_image_write_sig() - write the signature to a FIT
Simon Glass10a1eca2013-05-07 06:11:54 +000098 *
Simon Glassfbabc0f2013-06-13 15:10:01 -070099 * This writes the signature and signer data to the FIT.
100 *
101 * @fit: pointer to the FIT format image header
102 * @noffset: hash node offset
103 * @value: signature value to be set
104 * @value_len: signature value length
105 * @comment: Text comment to write (NULL for none)
106 *
107 * returns
108 * 0, on success
109 * -FDT_ERR_..., on failure
110 */
111static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
112 int value_len, const char *comment, const char *region_prop,
Jan Kiszka4043f322022-01-14 10:21:19 +0100113 int region_proplen, const char *cmdname, const char *algo_name)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700114{
115 int string_size;
116 int ret;
117
118 /*
119 * Get the current string size, before we update the FIT and add
120 * more
121 */
122 string_size = fdt_size_dt_strings(fit);
123
124 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
125 if (!ret) {
126 ret = fdt_setprop_string(fit, noffset, "signer-name",
127 "mkimage");
128 }
129 if (!ret) {
130 ret = fdt_setprop_string(fit, noffset, "signer-version",
131 PLAIN_VERSION);
132 }
133 if (comment && !ret)
134 ret = fdt_setprop_string(fit, noffset, "comment", comment);
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000135 if (!ret) {
136 time_t timestamp = imagetool_get_source_date(cmdname,
137 time(NULL));
Ming Liuf75f1d72021-05-31 09:04:51 +0200138 uint32_t t = cpu_to_uimage(timestamp);
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000139
Ming Liuf75f1d72021-05-31 09:04:51 +0200140 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
141 sizeof(uint32_t));
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000142 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700143 if (region_prop && !ret) {
144 uint32_t strdata[2];
145
146 ret = fdt_setprop(fit, noffset, "hashed-nodes",
147 region_prop, region_proplen);
Teddy Reed9767bf42018-06-09 11:45:20 -0400148 /* This is a legacy offset, it is unused, and must remain 0. */
Simon Glassfbabc0f2013-06-13 15:10:01 -0700149 strdata[0] = 0;
150 strdata[1] = cpu_to_fdt32(string_size);
151 if (!ret) {
152 ret = fdt_setprop(fit, noffset, "hashed-strings",
153 strdata, sizeof(strdata));
154 }
155 }
Jan Kiszka4043f322022-01-14 10:21:19 +0100156 if (algo_name && !ret)
157 ret = fdt_setprop_string(fit, noffset, "algo", algo_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700158
159 return ret;
160}
161
162static int fit_image_setup_sig(struct image_sign_info *info,
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600163 const char *keydir, const char *keyfile, void *fit,
164 const char *image_name, int noffset, const char *require_keys,
Jan Kiszka4043f322022-01-14 10:21:19 +0100165 const char *engine_id, const char *algo_name)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700166{
167 const char *node_name;
Philippe Reynes12468352018-11-14 13:51:00 +0100168 const char *padding_name;
Simon Glassfbabc0f2013-06-13 15:10:01 -0700169
170 node_name = fit_get_name(fit, noffset, NULL);
Jan Kiszka4043f322022-01-14 10:21:19 +0100171 if (!algo_name) {
172 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
173 printf("Can't get algo property for '%s' signature node in '%s' image node\n",
174 node_name, image_name);
175 return -1;
176 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700177 }
178
Philippe Reynes12468352018-11-14 13:51:00 +0100179 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
180
Simon Glassfbabc0f2013-06-13 15:10:01 -0700181 memset(info, '\0', sizeof(*info));
182 info->keydir = keydir;
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600183 info->keyfile = keyfile;
Simon Glassd7aabcc2020-03-18 11:44:06 -0600184 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700185 info->fit = fit;
186 info->node_offset = noffset;
Masahiro Yamadac1f137b2017-10-27 13:25:21 +0900187 info->name = strdup(algo_name);
Andrew Duda6616c822016-11-08 18:53:41 +0000188 info->checksum = image_get_checksum_algo(algo_name);
189 info->crypto = image_get_crypto_algo(algo_name);
Philippe Reynes12468352018-11-14 13:51:00 +0100190 info->padding = image_get_padding_algo(padding_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700191 info->require_keys = require_keys;
George McCollister23d14892017-01-06 13:14:17 -0600192 info->engine_id = engine_id;
Andrew Duda6616c822016-11-08 18:53:41 +0000193 if (!info->checksum || !info->crypto) {
Simon Glassfbabc0f2013-06-13 15:10:01 -0700194 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
195 algo_name, node_name, image_name);
196 return -1;
197 }
198
199 return 0;
200}
201
202/**
203 * fit_image_process_sig- Process a single subnode of the images/ node
204 *
205 * Check each subnode and process accordingly. For signature nodes we
Simon Glass6a0efc82021-11-12 12:28:06 -0700206 * generate a signed hash of the supplied data and store it in the node.
Simon Glassfbabc0f2013-06-13 15:10:01 -0700207 *
208 * @keydir: Directory containing keys to use for signing
Simon Glass6a0efc82021-11-12 12:28:06 -0700209 * @keydest: Destination FDT blob to write public keys into (NULL if none)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700210 * @fit: pointer to the FIT format image header
Simon Glass6a0efc82021-11-12 12:28:06 -0700211 * @image_name: name of image being processed (used to display errors)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700212 * @noffset: subnode offset
213 * @data: data to process
214 * @size: size of data in bytes
215 * @comment: Comment to add to signature nodes
216 * @require_keys: Mark all keys as 'required'
George McCollister23d14892017-01-06 13:14:17 -0600217 * @engine_id: Engine to use for signing
Simon Glass89c3fb62021-11-12 12:28:12 -0700218 * Return: keydest node if @keydest is non-NULL, else 0 if none; -ve error code
219 * on failure
Simon Glassfbabc0f2013-06-13 15:10:01 -0700220 */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600221static int fit_image_process_sig(const char *keydir, const char *keyfile,
222 void *keydest, void *fit, const char *image_name,
Simon Glassfbabc0f2013-06-13 15:10:01 -0700223 int noffset, const void *data, size_t size,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000224 const char *comment, int require_keys, const char *engine_id,
Jan Kiszka4043f322022-01-14 10:21:19 +0100225 const char *cmdname, const char *algo_name)
Simon Glassfbabc0f2013-06-13 15:10:01 -0700226{
227 struct image_sign_info info;
228 struct image_region region;
229 const char *node_name;
230 uint8_t *value;
231 uint value_len;
232 int ret;
233
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600234 if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
235 noffset, require_keys ? "image" : NULL,
Jan Kiszka4043f322022-01-14 10:21:19 +0100236 engine_id, algo_name))
Simon Glassfbabc0f2013-06-13 15:10:01 -0700237 return -1;
238
239 node_name = fit_get_name(fit, noffset, NULL);
240 region.data = data;
241 region.size = size;
Andrew Duda6616c822016-11-08 18:53:41 +0000242 ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700243 if (ret) {
244 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
245 node_name, image_name, ret);
246
247 /* We allow keys to be missing */
248 if (ret == -ENOENT)
249 return 0;
250 return -1;
251 }
252
253 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Jan Kiszka4043f322022-01-14 10:21:19 +0100254 NULL, 0, cmdname, algo_name);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700255 if (ret) {
Simon Glass802aa822014-06-02 22:04:53 -0600256 if (ret == -FDT_ERR_NOSPACE)
257 return -ENOSPC;
258 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
Simon Glassfbabc0f2013-06-13 15:10:01 -0700259 node_name, image_name, fdt_strerror(ret));
260 return -1;
261 }
262 free(value);
263
264 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glassd7aabcc2020-03-18 11:44:06 -0600265 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700266
mario.six@gdsys.ccdc4053a2016-07-22 08:58:40 +0200267 /*
268 * Write the public key into the supplied FDT file; this might fail
mario.six@gdsys.ccd019e152016-07-19 11:07:06 +0200269 * several times, since we try signing with successively increasing
mario.six@gdsys.ccdc4053a2016-07-22 08:58:40 +0200270 * size values
271 */
Masahiro Yamada01486762017-10-27 15:04:20 +0900272 if (keydest) {
273 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glass94336dc2021-11-12 12:28:11 -0700274 if (ret < 0) {
Masahiro Yamada01486762017-10-27 15:04:20 +0900275 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
276 node_name, image_name);
277 return ret;
278 }
Simon Glass89c3fb62021-11-12 12:28:12 -0700279 /* Return the node that was written to */
280 return ret;
Masahiro Yamada01486762017-10-27 15:04:20 +0900281 }
Simon Glassfbabc0f2013-06-13 15:10:01 -0700282
283 return 0;
284}
285
Philippe Reynes3148e422019-12-18 18:25:41 +0100286static int fit_image_read_data(char *filename, unsigned char *data,
287 int expected_size)
288{
289 struct stat sbuf;
290 int fd, ret = -1;
291 ssize_t n;
292
293 /* Open file */
294 fd = open(filename, O_RDONLY | O_BINARY);
295 if (fd < 0) {
296 printf("Can't open file %s (err=%d => %s)\n",
297 filename, errno, strerror(errno));
298 return -1;
299 }
300
301 /* Compute file size */
302 if (fstat(fd, &sbuf) < 0) {
303 printf("Can't fstat file %s (err=%d => %s)\n",
304 filename, errno, strerror(errno));
305 goto err;
306 }
307
308 /* Check file size */
309 if (sbuf.st_size != expected_size) {
Heinrich Schuchardtd697a2d2020-10-08 20:51:24 +0200310 printf("File %s don't have the expected size (size=%lld, expected=%d)\n",
311 filename, (long long)sbuf.st_size, expected_size);
Philippe Reynes3148e422019-12-18 18:25:41 +0100312 goto err;
313 }
314
315 /* Read data */
316 n = read(fd, data, sbuf.st_size);
317 if (n < 0) {
318 printf("Can't read file %s (err=%d => %s)\n",
319 filename, errno, strerror(errno));
320 goto err;
321 }
322
323 /* Check that we have read all the file */
324 if (n != sbuf.st_size) {
Vagrant Cascadian396e4ed2021-09-28 10:11:46 -0700325 printf("Can't read all file %s (read %zd bytes, expected %lld)\n",
Heinrich Schuchardtd697a2d2020-10-08 20:51:24 +0200326 filename, n, (long long)sbuf.st_size);
Philippe Reynes3148e422019-12-18 18:25:41 +0100327 goto err;
328 }
329
330 ret = 0;
331
332err:
333 close(fd);
334 return ret;
335}
336
Philippe Reynes64d67c42020-09-17 15:01:46 +0200337static int get_random_data(void *data, int size)
338{
339 unsigned char *tmp = data;
340 struct timespec date;
Simon Glass7a1745c2021-05-13 19:39:20 -0600341 int i, ret;
Philippe Reynes64d67c42020-09-17 15:01:46 +0200342
343 if (!tmp) {
344 printf("%s: pointer data is NULL\n", __func__);
345 ret = -1;
346 goto out;
347 }
348
349 ret = clock_gettime(CLOCK_MONOTONIC, &date);
Simon Glass7a1745c2021-05-13 19:39:20 -0600350 if (ret) {
351 printf("%s: clock_gettime has failed (%s)\n", __func__,
352 strerror(errno));
Philippe Reynes64d67c42020-09-17 15:01:46 +0200353 goto out;
354 }
355
Philippe Reynes95f2ab22020-11-13 16:37:46 +0100356 srandom(date.tv_nsec);
Philippe Reynes64d67c42020-09-17 15:01:46 +0200357
358 for (i = 0; i < size; i++) {
Philippe Reynes95f2ab22020-11-13 16:37:46 +0100359 *tmp = random() & 0xff;
Philippe Reynes64d67c42020-09-17 15:01:46 +0200360 tmp++;
361 }
362
363 out:
364 return ret;
365}
366
Philippe Reynes3148e422019-12-18 18:25:41 +0100367static int fit_image_setup_cipher(struct image_cipher_info *info,
368 const char *keydir, void *fit,
369 const char *image_name, int image_noffset,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000370 int noffset)
Philippe Reynes3148e422019-12-18 18:25:41 +0100371{
372 char *algo_name;
373 char filename[128];
374 int ret = -1;
375
376 if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000377 printf("Can't get algo name for cipher in image '%s'\n",
378 image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100379 goto out;
380 }
381
382 info->keydir = keydir;
383
384 /* Read the key name */
Simon Glassd7aabcc2020-03-18 11:44:06 -0600385 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Philippe Reynes3148e422019-12-18 18:25:41 +0100386 if (!info->keyname) {
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000387 printf("Can't get key name for cipher in image '%s'\n",
388 image_name);
Philippe Reynes3148e422019-12-18 18:25:41 +0100389 goto out;
390 }
391
Philippe Reynes64d67c42020-09-17 15:01:46 +0200392 /*
393 * Read the IV name
394 *
395 * If this property is not provided then mkimage will generate
396 * a random IV and store it in the FIT image
397 */
Philippe Reynes3148e422019-12-18 18:25:41 +0100398 info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
Philippe Reynes3148e422019-12-18 18:25:41 +0100399
400 info->fit = fit;
401 info->node_noffset = noffset;
402 info->name = algo_name;
403
404 info->cipher = image_get_cipher_algo(algo_name);
405 if (!info->cipher) {
406 printf("Can't get algo for cipher '%s'\n", image_name);
407 goto out;
408 }
409
410 /* Read the key in the file */
411 snprintf(filename, sizeof(filename), "%s/%s%s",
412 info->keydir, info->keyname, ".bin");
413 info->key = malloc(info->cipher->key_len);
414 if (!info->key) {
415 printf("Can't allocate memory for key\n");
416 ret = -1;
417 goto out;
418 }
419 ret = fit_image_read_data(filename, (unsigned char *)info->key,
420 info->cipher->key_len);
421 if (ret < 0)
422 goto out;
423
Philippe Reynes3148e422019-12-18 18:25:41 +0100424 info->iv = malloc(info->cipher->iv_len);
425 if (!info->iv) {
426 printf("Can't allocate memory for iv\n");
427 ret = -1;
428 goto out;
429 }
Philippe Reynes64d67c42020-09-17 15:01:46 +0200430
431 if (info->ivname) {
432 /* Read the IV in the file */
433 snprintf(filename, sizeof(filename), "%s/%s%s",
434 info->keydir, info->ivname, ".bin");
435 ret = fit_image_read_data(filename, (unsigned char *)info->iv,
436 info->cipher->iv_len);
437 } else {
438 /* Generate an ramdom IV */
439 ret = get_random_data((void *)info->iv, info->cipher->iv_len);
440 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100441
442 out:
443 return ret;
444}
445
446int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
447 const void *data, size_t size,
448 unsigned char *data_ciphered, int data_ciphered_len)
449{
450 int ret = -1;
451
Patrick Oppenlanderbe472112020-07-30 14:22:14 +1000452 /* Replace data with ciphered data */
Philippe Reynes3148e422019-12-18 18:25:41 +0100453 ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
454 data_ciphered, data_ciphered_len);
Patrick Oppenlanderbe472112020-07-30 14:22:14 +1000455 if (ret == -FDT_ERR_NOSPACE) {
456 ret = -ENOSPC;
457 goto out;
458 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100459 if (ret) {
Patrick Oppenlanderbe472112020-07-30 14:22:14 +1000460 printf("Can't replace data with ciphered data (err = %d)\n", ret);
Philippe Reynes3148e422019-12-18 18:25:41 +0100461 goto out;
462 }
463
464 /* add non ciphered data size */
465 ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
Patrick Oppenlanderbe472112020-07-30 14:22:14 +1000466 if (ret == -FDT_ERR_NOSPACE) {
467 ret = -ENOSPC;
468 goto out;
469 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100470 if (ret) {
471 printf("Can't add unciphered data size (err = %d)\n", ret);
472 goto out;
473 }
474
475 out:
476 return ret;
477}
478
479static int
480fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
481 const char *image_name, int image_noffset,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000482 int node_noffset, const void *data, size_t size,
Philippe Reynes3148e422019-12-18 18:25:41 +0100483 const char *cmdname)
484{
485 struct image_cipher_info info;
486 unsigned char *data_ciphered = NULL;
487 int data_ciphered_len;
488 int ret;
489
490 memset(&info, 0, sizeof(info));
491
492 ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000493 image_noffset, node_noffset);
Philippe Reynes3148e422019-12-18 18:25:41 +0100494 if (ret)
495 goto out;
496
497 ret = info.cipher->encrypt(&info, data, size,
498 &data_ciphered, &data_ciphered_len);
499 if (ret)
500 goto out;
501
502 /*
503 * Write the public key into the supplied FDT file; this might fail
504 * several times, since we try signing with successively increasing
505 * size values
Philippe Reynes64d67c42020-09-17 15:01:46 +0200506 * And, if needed, write the iv in the FIT file
Philippe Reynes3148e422019-12-18 18:25:41 +0100507 */
508 if (keydest) {
Philippe Reynes64d67c42020-09-17 15:01:46 +0200509 ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
Philippe Reynes3148e422019-12-18 18:25:41 +0100510 if (ret) {
511 printf("Failed to add verification data for cipher '%s' in image '%s'\n",
512 info.keyname, image_name);
513 goto out;
514 }
515 }
516
517 ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
518 data, size,
519 data_ciphered, data_ciphered_len);
520
521 out:
522 free(data_ciphered);
523 free((void *)info.key);
524 free((void *)info.iv);
525 return ret;
526}
527
528int fit_image_cipher_data(const char *keydir, void *keydest,
529 void *fit, int image_noffset, const char *comment,
530 int require_keys, const char *engine_id,
531 const char *cmdname)
532{
533 const char *image_name;
534 const void *data;
535 size_t size;
Patrick Oppenlandera4fafb32020-07-30 14:22:15 +1000536 int cipher_node_offset, len;
Philippe Reynes3148e422019-12-18 18:25:41 +0100537
538 /* Get image name */
539 image_name = fit_get_name(fit, image_noffset, NULL);
540 if (!image_name) {
541 printf("Can't get image name\n");
542 return -1;
543 }
544
545 /* Get image data and data length */
546 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
547 printf("Can't get image data/size\n");
548 return -1;
549 }
550
Patrick Oppenlandera4fafb32020-07-30 14:22:15 +1000551 /*
552 * Don't cipher ciphered data.
553 *
554 * If the data-size-unciphered property is present the data for this
555 * image is already encrypted. This is important as 'mkimage -F' can be
556 * run multiple times on a FIT image.
557 */
558 if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len))
559 return 0;
560 if (len != -FDT_ERR_NOTFOUND) {
561 printf("Failure testing for data-size-unciphered\n");
562 return -1;
563 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100564
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000565 /* Process cipher node if present */
566 cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
567 FIT_CIPHER_NODENAME);
568 if (cipher_node_offset == -FDT_ERR_NOTFOUND)
569 return 0;
570 if (cipher_node_offset < 0) {
571 printf("Failure getting cipher node\n");
572 return -1;
Philippe Reynes3148e422019-12-18 18:25:41 +0100573 }
Patrick Oppenlanderd586fc32020-07-30 14:22:13 +1000574 if (!IMAGE_ENABLE_ENCRYPT || !keydir)
575 return 0;
576 return fit_image_process_cipher(keydir, keydest, fit, image_name,
577 image_noffset, cipher_node_offset, data, size, cmdname);
Philippe Reynes3148e422019-12-18 18:25:41 +0100578}
579
Simon Glassfbabc0f2013-06-13 15:10:01 -0700580/**
581 * fit_image_add_verification_data() - calculate/set verig. data for image node
582 *
583 * This adds hash and signature values for an component image node.
Simon Glassee382652013-05-07 06:12:01 +0000584 *
585 * All existing hash subnodes are checked, if algorithm property is set to
586 * one of the supported hash algorithms, hash value is computed and
587 * corresponding hash node property is set, for example:
Simon Glass10a1eca2013-05-07 06:11:54 +0000588 *
589 * Input component image node structure:
590 *
Andre Przywara3234ecd2017-12-04 02:05:10 +0000591 * o image-1 (at image_noffset)
Simon Glass10a1eca2013-05-07 06:11:54 +0000592 * | - data = [binary data]
Andre Przywara3234ecd2017-12-04 02:05:10 +0000593 * o hash-1
Simon Glass10a1eca2013-05-07 06:11:54 +0000594 * |- algo = "sha1"
595 *
596 * Output component image node structure:
597 *
Andre Przywara3234ecd2017-12-04 02:05:10 +0000598 * o image-1 (at image_noffset)
Simon Glass10a1eca2013-05-07 06:11:54 +0000599 * | - data = [binary data]
Andre Przywara3234ecd2017-12-04 02:05:10 +0000600 * o hash-1
Simon Glass10a1eca2013-05-07 06:11:54 +0000601 * |- algo = "sha1"
602 * |- value = sha1(data)
603 *
Simon Glassee382652013-05-07 06:12:01 +0000604 * For signature details, please see doc/uImage.FIT/signature.txt
605 *
Simon Glassfbabc0f2013-06-13 15:10:01 -0700606 * @keydir Directory containing *.key and *.crt files (or NULL)
607 * @keydest FDT Blob to write public keys into (NULL if none)
Simon Glassee382652013-05-07 06:12:01 +0000608 * @fit: Pointer to the FIT format image header
609 * @image_noffset: Requested component image node
Simon Glassfbabc0f2013-06-13 15:10:01 -0700610 * @comment: Comment to add to signature nodes
611 * @require_keys: Mark all keys as 'required'
George McCollister23d14892017-01-06 13:14:17 -0600612 * @engine_id: Engine to use for signing
Simon Glassee382652013-05-07 06:12:01 +0000613 * @return: 0 on success, <0 on failure
Simon Glass10a1eca2013-05-07 06:11:54 +0000614 */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600615int fit_image_add_verification_data(const char *keydir, const char *keyfile,
616 void *keydest, void *fit, int image_noffset,
617 const char *comment, int require_keys, const char *engine_id,
Jan Kiszka4043f322022-01-14 10:21:19 +0100618 const char *cmdname, const char* algo_name)
Simon Glass10a1eca2013-05-07 06:11:54 +0000619{
Simon Glassee382652013-05-07 06:12:01 +0000620 const char *image_name;
Simon Glass10a1eca2013-05-07 06:11:54 +0000621 const void *data;
622 size_t size;
Simon Glass10a1eca2013-05-07 06:11:54 +0000623 int noffset;
Simon Glass10a1eca2013-05-07 06:11:54 +0000624
625 /* Get image data and data length */
626 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
627 printf("Can't get image data/size\n");
628 return -1;
629 }
630
Simon Glass25d6e6b2013-05-07 06:11:55 +0000631 image_name = fit_get_name(fit, image_noffset, NULL);
632
Simon Glass10a1eca2013-05-07 06:11:54 +0000633 /* Process all hash subnodes of the component image node */
Simon Glassee382652013-05-07 06:12:01 +0000634 for (noffset = fdt_first_subnode(fit, image_noffset);
635 noffset >= 0;
636 noffset = fdt_next_subnode(fit, noffset)) {
637 const char *node_name;
638 int ret = 0;
639
640 /*
641 * Check subnode name, must be equal to "hash" or "signature".
642 * Multiple hash nodes require unique unit node
Andre Przywara3234ecd2017-12-04 02:05:10 +0000643 * names, e.g. hash-1, hash-2, signature-1, etc.
Simon Glassee382652013-05-07 06:12:01 +0000644 */
645 node_name = fit_get_name(fit, noffset, NULL);
646 if (!strncmp(node_name, FIT_HASH_NODENAME,
647 strlen(FIT_HASH_NODENAME))) {
648 ret = fit_image_process_hash(fit, image_name, noffset,
649 data, size);
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600650 } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) &&
Simon Glassfbabc0f2013-06-13 15:10:01 -0700651 !strncmp(node_name, FIT_SIG_NODENAME,
652 strlen(FIT_SIG_NODENAME))) {
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600653 ret = fit_image_process_sig(keydir, keyfile, keydest,
Simon Glassfbabc0f2013-06-13 15:10:01 -0700654 fit, image_name, noffset, data, size,
Jan Kiszka4043f322022-01-14 10:21:19 +0100655 comment, require_keys, engine_id, cmdname,
656 algo_name);
Simon Glass10a1eca2013-05-07 06:11:54 +0000657 }
Simon Glass89c3fb62021-11-12 12:28:12 -0700658 if (ret < 0)
Simon Glassec539f72016-07-03 09:40:44 -0600659 return ret;
Simon Glassee382652013-05-07 06:12:01 +0000660 }
661
662 return 0;
663}
664
Simon Glass56ab8d62013-06-13 15:10:09 -0700665struct strlist {
666 int count;
667 char **strings;
668};
669
670static void strlist_init(struct strlist *list)
671{
672 memset(list, '\0', sizeof(*list));
673}
674
675static void strlist_free(struct strlist *list)
676{
677 int i;
678
679 for (i = 0; i < list->count; i++)
680 free(list->strings[i]);
681 free(list->strings);
682}
683
684static int strlist_add(struct strlist *list, const char *str)
685{
686 char *dup;
687
688 dup = strdup(str);
689 list->strings = realloc(list->strings,
690 (list->count + 1) * sizeof(char *));
691 if (!list || !str)
692 return -1;
693 list->strings[list->count++] = dup;
694
695 return 0;
696}
697
Simon Glass6a0efc82021-11-12 12:28:06 -0700698static const char *fit_config_get_image_list(const void *fit, int noffset,
699 int *lenp, int *allow_missingp)
Simon Glass56ab8d62013-06-13 15:10:09 -0700700{
701 static const char default_list[] = FIT_KERNEL_PROP "\0"
702 FIT_FDT_PROP;
703 const char *prop;
704
Simon Glass6a0efc82021-11-12 12:28:06 -0700705 /* If there is an "sign-image" property, use that */
Simon Glass56ab8d62013-06-13 15:10:09 -0700706 prop = fdt_getprop(fit, noffset, "sign-images", lenp);
707 if (prop) {
708 *allow_missingp = 0;
709 return *lenp ? prop : NULL;
710 }
711
712 /* Default image list */
713 *allow_missingp = 1;
714 *lenp = sizeof(default_list);
715
716 return default_list;
717}
718
Simon Glass6a0efc82021-11-12 12:28:06 -0700719/**
720 * fit_config_add_hash() - Add a list of nodes to hash for an image
721 *
722 * This adds a list of paths to image nodes (as referred to by a particular
723 * offset) that need to be hashed, to protect a configuration
724 *
725 * @fit: Pointer to the FIT format image header
726 * @image_noffset: Offset of image to process (e.g. /images/kernel-1)
727 * @node_inc: List of nodes to add to
728 * @conf_name Configuration-node name, child of /configurations node (only
729 * used for error messages)
730 * @sig_name Signature-node name (only used for error messages)
731 * @iname: Name of image being processed (e.g. "kernel-1" (only used
732 * for error messages)
733 */
734static int fit_config_add_hash(const void *fit, int image_noffset,
735 struct strlist *node_inc, const char *conf_name,
736 const char *sig_name, const char *iname)
Philippe Reynes856bcc82020-11-24 14:39:47 +0100737{
Simon Glass888c0c52021-11-12 12:28:07 -0700738 char path[200];
Philippe Reynes856bcc82020-11-24 14:39:47 +0100739 int noffset;
740 int hash_count;
741 int ret;
742
743 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
744 if (ret < 0)
745 goto err_path;
746 if (strlist_add(node_inc, path))
747 goto err_mem;
748
Philippe Reynes856bcc82020-11-24 14:39:47 +0100749 /* Add all this image's hashes */
750 hash_count = 0;
751 for (noffset = fdt_first_subnode(fit, image_noffset);
752 noffset >= 0;
753 noffset = fdt_next_subnode(fit, noffset)) {
754 const char *name = fit_get_name(fit, noffset, NULL);
755
756 if (strncmp(name, FIT_HASH_NODENAME,
757 strlen(FIT_HASH_NODENAME)))
758 continue;
759 ret = fdt_get_path(fit, noffset, path, sizeof(path));
760 if (ret < 0)
761 goto err_path;
762 if (strlist_add(node_inc, path))
763 goto err_mem;
764 hash_count++;
765 }
766
767 if (!hash_count) {
768 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
769 conf_name, sig_name, iname);
770 return -ENOMSG;
771 }
772
773 /* Add this image's cipher node if present */
774 noffset = fdt_subnode_offset(fit, image_noffset,
775 FIT_CIPHER_NODENAME);
776 if (noffset != -FDT_ERR_NOTFOUND) {
777 if (noffset < 0) {
778 printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n",
779 conf_name, sig_name, iname,
780 fdt_strerror(noffset));
781 return -EIO;
782 }
783 ret = fdt_get_path(fit, noffset, path, sizeof(path));
784 if (ret < 0)
785 goto err_path;
786 if (strlist_add(node_inc, path))
787 goto err_mem;
788 }
789
790 return 0;
791
792err_mem:
793 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
794 sig_name);
795 return -ENOMEM;
796
797err_path:
798 printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
799 iname, conf_name, sig_name, fdt_strerror(ret));
800 return -ENOENT;
801}
802
Simon Glass6a0efc82021-11-12 12:28:06 -0700803/**
804 * fit_config_get_hash_list() - Get the regions to sign
805 *
806 * This calculates a list of nodes to hash for this particular configuration,
807 * returning it as a string list (struct strlist, not a devicetree string list)
808 *
809 * @fit: Pointer to the FIT format image header
810 * @conf_noffset: Offset of configuration node to sign (child of
811 * /configurations node)
812 * @sig_offset: Offset of signature node containing info about how to sign it
813 * (child of 'signatures' node)
814 * @return 0 if OK, -ENOENT if an image referred to by the configuration cannot
815 * be found, -ENOMSG if ther were no images in the configuration
816 */
817static int fit_config_get_hash_list(const void *fit, int conf_noffset,
Simon Glass56ab8d62013-06-13 15:10:09 -0700818 int sig_offset, struct strlist *node_inc)
819{
820 int allow_missing;
821 const char *prop, *iname, *end;
822 const char *conf_name, *sig_name;
Philippe Reynes856bcc82020-11-24 14:39:47 +0100823 char name[200];
Simon Glass56ab8d62013-06-13 15:10:09 -0700824 int image_count;
825 int ret, len;
826
827 conf_name = fit_get_name(fit, conf_noffset, NULL);
828 sig_name = fit_get_name(fit, sig_offset, NULL);
829
830 /*
831 * Build a list of nodes we need to hash. We always need the root
832 * node and the configuration.
833 */
834 strlist_init(node_inc);
835 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
836 if (strlist_add(node_inc, "/") ||
837 strlist_add(node_inc, name))
838 goto err_mem;
839
840 /* Get a list of images that we intend to sign */
Heiko Schocherfdfd5202014-03-03 12:19:23 +0100841 prop = fit_config_get_image_list(fit, sig_offset, &len,
Simon Glass56ab8d62013-06-13 15:10:09 -0700842 &allow_missing);
843 if (!prop)
844 return 0;
845
846 /* Locate the images */
847 end = prop + len;
848 image_count = 0;
849 for (iname = prop; iname < end; iname += strlen(iname) + 1) {
Simon Glass56ab8d62013-06-13 15:10:09 -0700850 int image_noffset;
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100851 int index, max_index;
Simon Glass56ab8d62013-06-13 15:10:09 -0700852
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100853 max_index = fdt_stringlist_count(fit, conf_noffset, iname);
Simon Glass56ab8d62013-06-13 15:10:09 -0700854
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100855 for (index = 0; index < max_index; index++) {
856 image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset,
857 iname, index);
858
859 if (image_noffset < 0) {
860 printf("Failed to find image '%s' in configuration '%s/%s'\n",
861 iname, conf_name, sig_name);
862 if (allow_missing)
863 continue;
Simon Glass56ab8d62013-06-13 15:10:09 -0700864
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100865 return -ENOENT;
866 }
Patrick Oppenlander5964c3f2020-07-30 14:30:47 +1000867
Simon Glass6a0efc82021-11-12 12:28:06 -0700868 ret = fit_config_add_hash(fit, image_noffset, node_inc,
869 conf_name, sig_name, iname);
Philippe Reynes1f7d9a22020-11-24 14:39:48 +0100870 if (ret < 0)
871 return ret;
872
873 image_count++;
874 }
Simon Glass56ab8d62013-06-13 15:10:09 -0700875 }
876
877 if (!image_count) {
878 printf("Failed to find any images for configuration '%s/%s'\n",
879 conf_name, sig_name);
880 return -ENOMSG;
881 }
882
883 return 0;
884
885err_mem:
886 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
887 sig_name);
888 return -ENOMEM;
Simon Glass56ab8d62013-06-13 15:10:09 -0700889}
890
Simon Glass6a0efc82021-11-12 12:28:06 -0700891/**
892 * fit_config_get_regions() - Get the regions to sign
893 *
894 * This calculates a list of node to hash for this particular configuration,
895 * then finds which regions of the devicetree they correspond to.
896 *
897 * @fit: Pointer to the FIT format image header
898 * @conf_noffset: Offset of configuration node to sign (child of
899 * /configurations node)
900 * @sig_offset: Offset of signature node containing info about how to sign it
901 * (child of 'signatures' node)
902 * @regionp: Returns list of regions that need to be hashed (allocated; must be
903 * freed by the caller)
904 * @region_count: Returns number of regions
905 * @region_propp: Returns string-list property containing the list of nodes
906 * that correspond to the regions. Each entry is a full path to the node.
907 * This is in devicetree format, i.e. a \0 between each string. This is
908 * allocated and must be freed by the caller.
909 * @region_proplen: Returns length of *@@region_propp in bytes
910 * @return 0 if OK, -ENOMEM if out of memory, -EIO if the regions to hash could
911 * not be found, -EINVAL if no registers were found to hash
912 */
913static int fit_config_get_regions(const void *fit, int conf_noffset,
914 int sig_offset, struct image_region **regionp,
915 int *region_countp, char **region_propp,
916 int *region_proplen)
Simon Glass56ab8d62013-06-13 15:10:09 -0700917{
918 char * const exc_prop[] = {"data"};
919 struct strlist node_inc;
920 struct image_region *region;
921 struct fdt_region fdt_regions[100];
922 const char *conf_name, *sig_name;
923 char path[200];
924 int count, i;
925 char *region_prop;
926 int ret, len;
927
928 conf_name = fit_get_name(fit, conf_noffset, NULL);
Simon Glass6a0efc82021-11-12 12:28:06 -0700929 sig_name = fit_get_name(fit, sig_offset, NULL);
Simon Glass56ab8d62013-06-13 15:10:09 -0700930 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
931
932 /* Get a list of nodes we want to hash */
Simon Glass6a0efc82021-11-12 12:28:06 -0700933 ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset,
934 &node_inc);
Simon Glass56ab8d62013-06-13 15:10:09 -0700935 if (ret)
936 return ret;
937
938 /* Get a list of regions to hash */
939 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
940 exc_prop, ARRAY_SIZE(exc_prop),
941 fdt_regions, ARRAY_SIZE(fdt_regions),
942 path, sizeof(path), 1);
943 if (count < 0) {
944 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
945 sig_name, fdt_strerror(ret));
946 return -EIO;
947 }
948 if (count == 0) {
949 printf("No data to hash for configuration '%s/%s': %s\n",
950 conf_name, sig_name, fdt_strerror(ret));
951 return -EINVAL;
952 }
953
954 /* Build our list of data blocks */
955 region = fit_region_make_list(fit, fdt_regions, count, NULL);
956 if (!region) {
957 printf("Out of memory hashing configuration '%s/%s'\n",
958 conf_name, sig_name);
959 return -ENOMEM;
960 }
961
962 /* Create a list of all hashed properties */
963 debug("Hash nodes:\n");
964 for (i = len = 0; i < node_inc.count; i++) {
965 debug(" %s\n", node_inc.strings[i]);
966 len += strlen(node_inc.strings[i]) + 1;
967 }
968 region_prop = malloc(len);
969 if (!region_prop) {
970 printf("Out of memory setting up regions for configuration '%s/%s'\n",
971 conf_name, sig_name);
972 return -ENOMEM;
973 }
974 for (i = len = 0; i < node_inc.count;
975 len += strlen(node_inc.strings[i]) + 1, i++)
976 strcpy(region_prop + len, node_inc.strings[i]);
977 strlist_free(&node_inc);
978
979 *region_countp = count;
980 *regionp = region;
981 *region_propp = region_prop;
982 *region_proplen = len;
983
984 return 0;
985}
986
Simon Glass89c3fb62021-11-12 12:28:12 -0700987/**
988 * fit_config_process_sig - Process a single subnode of the configurations/ node
989 *
990 * Generate a signed hash of the supplied data and store it in the node.
991 *
992 * @keydir: Directory containing keys to use for signing
993 * @keydest: Destination FDT blob to write public keys into (NULL if none)
994 * @fit: pointer to the FIT format image header
995 * @conf_name name of config being processed (used to display errors)
996 * @conf_noffset: Offset of configuration node, e.g. '/configurations/conf-1'
997 * @noffset: subnode offset, e.g. '/configurations/conf-1/sig-1'
998 * @comment: Comment to add to signature nodes
999 * @require_keys: Mark all keys as 'required'
1000 * @engine_id: Engine to use for signing
1001 * @cmdname: Command name used when reporting errors
1002 * @return keydest node if @keydest is non-NULL, else 0 if none; -ve error code
1003 * on failure
1004 */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001005static int fit_config_process_sig(const char *keydir, const char *keyfile,
Simon Glass6a0efc82021-11-12 12:28:06 -07001006 void *keydest, void *fit, const char *conf_name,
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001007 int conf_noffset, int noffset, const char *comment,
Jan Kiszka4043f322022-01-14 10:21:19 +01001008 int require_keys, const char *engine_id, const char *cmdname,
1009 const char *algo_name)
Simon Glass56ab8d62013-06-13 15:10:09 -07001010{
1011 struct image_sign_info info;
1012 const char *node_name;
1013 struct image_region *region;
1014 char *region_prop;
1015 int region_proplen;
1016 int region_count;
1017 uint8_t *value;
1018 uint value_len;
1019 int ret;
1020
1021 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass6a0efc82021-11-12 12:28:06 -07001022 if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
1023 &region_count, &region_prop,
1024 &region_proplen))
Simon Glass56ab8d62013-06-13 15:10:09 -07001025 return -1;
1026
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001027 if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
Jan Kiszka4043f322022-01-14 10:21:19 +01001028 require_keys ? "conf" : NULL, engine_id,
1029 algo_name))
Simon Glass56ab8d62013-06-13 15:10:09 -07001030 return -1;
1031
Andrew Duda6616c822016-11-08 18:53:41 +00001032 ret = info.crypto->sign(&info, region, region_count, &value,
1033 &value_len);
Simon Glass56ab8d62013-06-13 15:10:09 -07001034 free(region);
1035 if (ret) {
1036 printf("Failed to sign '%s' signature node in '%s' conf node\n",
1037 node_name, conf_name);
1038
1039 /* We allow keys to be missing */
1040 if (ret == -ENOENT)
1041 return 0;
1042 return -1;
1043 }
1044
Simon Glass802aa822014-06-02 22:04:53 -06001045 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Jan Kiszka4043f322022-01-14 10:21:19 +01001046 region_prop, region_proplen, cmdname,
1047 algo_name);
Simon Glass802aa822014-06-02 22:04:53 -06001048 if (ret) {
1049 if (ret == -FDT_ERR_NOSPACE)
1050 return -ENOSPC;
1051 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
1052 node_name, conf_name, fdt_strerror(ret));
Simon Glass56ab8d62013-06-13 15:10:09 -07001053 return -1;
1054 }
1055 free(value);
1056 free(region_prop);
1057
1058 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glassd7aabcc2020-03-18 11:44:06 -06001059 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56ab8d62013-06-13 15:10:09 -07001060
1061 /* Write the public key into the supplied FDT file */
Simon Glass802aa822014-06-02 22:04:53 -06001062 if (keydest) {
Andrew Duda6616c822016-11-08 18:53:41 +00001063 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glass94336dc2021-11-12 12:28:11 -07001064 if (ret < 0) {
Masahiro Yamadafe391a72017-10-27 15:04:21 +09001065 printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
Simon Glass802aa822014-06-02 22:04:53 -06001066 node_name, conf_name);
Simon Glass802aa822014-06-02 22:04:53 -06001067 }
Simon Glass89c3fb62021-11-12 12:28:12 -07001068 return ret;
Simon Glass56ab8d62013-06-13 15:10:09 -07001069 }
1070
1071 return 0;
1072}
1073
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001074static int fit_config_add_verification_data(const char *keydir,
1075 const char *keyfile, void *keydest, void *fit, int conf_noffset,
1076 const char *comment, int require_keys, const char *engine_id,
Simon Glasse4607262021-11-12 12:28:13 -07001077 const char *cmdname, const char *algo_name,
1078 struct image_summary *summary)
Simon Glass56ab8d62013-06-13 15:10:09 -07001079{
1080 const char *conf_name;
1081 int noffset;
1082
1083 conf_name = fit_get_name(fit, conf_noffset, NULL);
1084
1085 /* Process all hash subnodes of the configuration node */
1086 for (noffset = fdt_first_subnode(fit, conf_noffset);
1087 noffset >= 0;
1088 noffset = fdt_next_subnode(fit, noffset)) {
1089 const char *node_name;
1090 int ret = 0;
1091
1092 node_name = fit_get_name(fit, noffset, NULL);
1093 if (!strncmp(node_name, FIT_SIG_NODENAME,
1094 strlen(FIT_SIG_NODENAME))) {
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001095 ret = fit_config_process_sig(keydir, keyfile, keydest,
Simon Glass56ab8d62013-06-13 15:10:09 -07001096 fit, conf_name, conf_noffset, noffset, comment,
Jan Kiszka4043f322022-01-14 10:21:19 +01001097 require_keys, engine_id, cmdname, algo_name);
Simon Glasse4607262021-11-12 12:28:13 -07001098 if (ret < 0)
1099 return ret;
1100
1101 summary->sig_offset = noffset;
1102 fdt_get_path(fit, noffset, summary->sig_path,
1103 sizeof(summary->sig_path));
1104
1105 if (keydest) {
1106 summary->keydest_offset = ret;
1107 fdt_get_path(keydest, ret,
1108 summary->keydest_path,
1109 sizeof(summary->keydest_path));
1110 }
Simon Glass56ab8d62013-06-13 15:10:09 -07001111 }
Simon Glass56ab8d62013-06-13 15:10:09 -07001112 }
1113
1114 return 0;
1115}
1116
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001117/*
1118 * 0) open file (open)
1119 * 1) read certificate (PEM_read_X509)
1120 * 2) get public key (X509_get_pubkey)
1121 * 3) provide der format (d2i_RSAPublicKey)
1122 */
1123static int read_pub_key(const char *keydir, const void *name,
1124 unsigned char **pubkey, int *pubkey_len)
1125{
1126 char path[1024];
1127 EVP_PKEY *key = NULL;
1128 X509 *cert;
1129 FILE *f;
1130 int ret;
1131
1132 memset(path, 0, 1024);
1133 snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name);
1134
1135 /* Open certificate file */
1136 f = fopen(path, "r");
1137 if (!f) {
1138 fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
1139 path, strerror(errno));
1140 return -EACCES;
1141 }
1142
1143 /* Read the certificate */
1144 cert = NULL;
1145 if (!PEM_read_X509(f, &cert, NULL, NULL)) {
1146 printf("Couldn't read certificate");
1147 ret = -EINVAL;
1148 goto err_cert;
1149 }
1150
1151 /* Get the public key from the certificate. */
1152 key = X509_get_pubkey(cert);
1153 if (!key) {
1154 printf("Couldn't read public key\n");
1155 ret = -EINVAL;
1156 goto err_pubkey;
1157 }
1158
1159 /* Get DER form */
1160 ret = i2d_PublicKey(key, pubkey);
1161 if (ret < 0) {
1162 printf("Couldn't get DER form\n");
1163 ret = -EINVAL;
1164 goto err_pubkey;
1165 }
1166
1167 *pubkey_len = ret;
1168 ret = 0;
1169
1170err_pubkey:
1171 X509_free(cert);
1172err_cert:
1173 fclose(f);
1174 return ret;
1175}
1176
1177int fit_pre_load_data(const char *keydir, void *keydest, void *fit)
1178{
1179 int pre_load_noffset;
1180 const void *algo_name;
1181 const void *key_name;
1182 unsigned char *pubkey = NULL;
1183 int ret, pubkey_len;
1184
1185 if (!keydir || !keydest || !fit)
1186 return 0;
1187
1188 /* Search node pre-load sig */
1189 pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH);
1190 if (pre_load_noffset < 0) {
1191 ret = 0;
1192 goto out;
1193 }
1194
1195 algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL);
1196 key_name = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL);
1197
1198 /* Check that all mandatory properties are present */
1199 if (!algo_name || !key_name) {
1200 if (!algo_name)
1201 printf("The property algo-name is missing in the node %s\n",
1202 IMAGE_PRE_LOAD_PATH);
1203 if (!key_name)
1204 printf("The property key-name is missing in the node %s\n",
1205 IMAGE_PRE_LOAD_PATH);
Mark Kettenisd4ba0b42022-04-26 19:24:38 +02001206 ret = -EINVAL;
Philippe Reynes3e3899c2022-03-28 22:57:02 +02001207 goto out;
1208 }
1209
1210 /* Read public key */
1211 ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len);
1212 if (ret < 0)
1213 goto out;
1214
1215 /* Add the public key to the device tree */
1216 ret = fdt_setprop(keydest, pre_load_noffset, "public-key",
1217 pubkey, pubkey_len);
1218 if (ret)
1219 printf("Can't set public-key in node %s (ret = %d)\n",
1220 IMAGE_PRE_LOAD_PATH, ret);
1221
1222 out:
1223 return ret;
1224}
1225
Philippe Reynes3148e422019-12-18 18:25:41 +01001226int fit_cipher_data(const char *keydir, void *keydest, void *fit,
1227 const char *comment, int require_keys,
1228 const char *engine_id, const char *cmdname)
1229{
1230 int images_noffset;
1231 int noffset;
1232 int ret;
1233
1234 /* Find images parent node offset */
1235 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1236 if (images_noffset < 0) {
1237 printf("Can't find images parent node '%s' (%s)\n",
1238 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1239 return images_noffset;
1240 }
1241
1242 /* Process its subnodes, print out component images details */
1243 for (noffset = fdt_first_subnode(fit, images_noffset);
1244 noffset >= 0;
1245 noffset = fdt_next_subnode(fit, noffset)) {
1246 /*
1247 * Direct child node of the images parent node,
1248 * i.e. component image node.
1249 */
1250 ret = fit_image_cipher_data(keydir, keydest,
1251 fit, noffset, comment,
1252 require_keys, engine_id,
1253 cmdname);
1254 if (ret)
1255 return ret;
1256 }
1257
1258 return 0;
1259}
1260
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001261int fit_add_verification_data(const char *keydir, const char *keyfile,
1262 void *keydest, void *fit, const char *comment,
1263 int require_keys, const char *engine_id,
Simon Glasse4607262021-11-12 12:28:13 -07001264 const char *cmdname, const char *algo_name,
1265 struct image_summary *summary)
Simon Glassee382652013-05-07 06:12:01 +00001266{
Simon Glass56ab8d62013-06-13 15:10:09 -07001267 int images_noffset, confs_noffset;
Simon Glassee382652013-05-07 06:12:01 +00001268 int noffset;
1269 int ret;
1270
1271 /* Find images parent node offset */
1272 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1273 if (images_noffset < 0) {
1274 printf("Can't find images parent node '%s' (%s)\n",
1275 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1276 return images_noffset;
1277 }
1278
1279 /* Process its subnodes, print out component images details */
1280 for (noffset = fdt_first_subnode(fit, images_noffset);
1281 noffset >= 0;
1282 noffset = fdt_next_subnode(fit, noffset)) {
1283 /*
1284 * Direct child node of the images parent node,
1285 * i.e. component image node.
1286 */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001287 ret = fit_image_add_verification_data(keydir, keyfile, keydest,
Alex Kiernan697fcdc2018-06-20 20:10:52 +00001288 fit, noffset, comment, require_keys, engine_id,
Jan Kiszka4043f322022-01-14 10:21:19 +01001289 cmdname, algo_name);
Simon Glassee382652013-05-07 06:12:01 +00001290 if (ret)
1291 return ret;
Simon Glass10a1eca2013-05-07 06:11:54 +00001292 }
1293
Simon Glass56ab8d62013-06-13 15:10:09 -07001294 /* If there are no keys, we can't sign configurations */
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001295 if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile))
Simon Glass56ab8d62013-06-13 15:10:09 -07001296 return 0;
1297
1298 /* Find configurations parent node offset */
1299 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1300 if (confs_noffset < 0) {
1301 printf("Can't find images parent node '%s' (%s)\n",
Heiko Schocher441f48d2014-08-11 11:02:17 +02001302 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Simon Glass56ab8d62013-06-13 15:10:09 -07001303 return -ENOENT;
1304 }
1305
1306 /* Process its subnodes, print out component images details */
1307 for (noffset = fdt_first_subnode(fit, confs_noffset);
1308 noffset >= 0;
1309 noffset = fdt_next_subnode(fit, noffset)) {
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -06001310 ret = fit_config_add_verification_data(keydir, keyfile, keydest,
Simon Glass56ab8d62013-06-13 15:10:09 -07001311 fit, noffset, comment,
George McCollister23d14892017-01-06 13:14:17 -06001312 require_keys,
Jan Kiszka4043f322022-01-14 10:21:19 +01001313 engine_id, cmdname,
Simon Glasse4607262021-11-12 12:28:13 -07001314 algo_name, summary);
Simon Glass56ab8d62013-06-13 15:10:09 -07001315 if (ret)
1316 return ret;
1317 }
1318
Simon Glass10a1eca2013-05-07 06:11:54 +00001319 return 0;
1320}
Heiko Schocherd7b42322014-03-03 12:19:30 +01001321
1322#ifdef CONFIG_FIT_SIGNATURE
Simon Glass05712322020-03-18 11:44:03 -06001323int fit_check_sign(const void *fit, const void *key,
1324 const char *fit_uname_config)
Heiko Schocherd7b42322014-03-03 12:19:30 +01001325{
1326 int cfg_noffset;
1327 int ret;
1328
Simon Glass05712322020-03-18 11:44:03 -06001329 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
Heiko Schocherd7b42322014-03-03 12:19:30 +01001330 if (!cfg_noffset)
1331 return -1;
1332
Simon Glassa559bb22020-03-18 11:43:56 -06001333 printf("Verifying Hash Integrity for node '%s'... ",
1334 fdt_get_name(fit, cfg_noffset, NULL));
Simon Glassa51991d2014-06-12 07:24:53 -06001335 ret = fit_config_verify(fit, cfg_noffset);
1336 if (ret)
1337 return ret;
Simon Glass05712322020-03-18 11:44:03 -06001338 printf("Verified OK, loading images\n");
Simon Glassa51991d2014-06-12 07:24:53 -06001339 ret = bootm_host_load_images(fit, cfg_noffset);
1340
Heiko Schocherd7b42322014-03-03 12:19:30 +01001341 return ret;
1342}
1343#endif