blob: 4a24dee8153c0514f18dcafad4b15c913c919e8d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass604f23d2013-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 Glass604f23d2013-05-07 06:11:54 +00009 */
10
11#include "mkimage.h"
Simon Glassce1400f2014-06-12 07:24:53 -060012#include <bootm.h>
Masahiro Yamada64045a62020-04-16 18:30:18 +090013#include <fdt_region.h>
Simon Glass604f23d2013-05-07 06:11:54 +000014#include <image.h>
Simon Glass56518e72013-06-13 15:10:01 -070015#include <version.h>
Simon Glass604f23d2013-05-07 06:11:54 +000016
Philippe Reynes6e052d12022-03-28 22:57:02 +020017#include <openssl/pem.h>
18#include <openssl/evp.h>
19
Simon Glass604f23d2013-05-07 06:11:54 +000020/**
Simon Glassb7260912013-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 Glass1152a052016-07-03 09:40:44 -060044 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
Simon Glassb7260912013-05-07 06:11:56 +000045 }
46
47 return 0;
48}
49
50/**
Simon Glass94e5fa42013-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 Glass70e6bcc2021-11-12 12:28:06 -070054 * a hash of the supplied data and store it in the node.
Simon Glass94e5fa42013-05-07 06:11:55 +000055 *
56 * @fit: pointer to the FIT format image header
Simon Glass70e6bcc2021-11-12 12:28:06 -070057 * @image_name: name of image being processed (used to display errors)
Simon Glass94e5fa42013-05-07 06:11:55 +000058 * @noffset: subnode offset
59 * @data: data to process
60 * @size: size of data in bytes
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010061 * Return: 0 if ok, -1 on error
Simon Glass94e5fa42013-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 Glassbbb467d2013-05-07 06:12:01 +000067 const char *node_name;
Simon Glass94e5fa42013-05-07 06:11:55 +000068 int value_len;
Jan Kiszka4550ce92022-01-14 10:21:17 +010069 const char *algo;
Simon Glass1152a052016-07-03 09:40:44 -060070 int ret;
Simon Glass94e5fa42013-05-07 06:11:55 +000071
Simon Glassbbb467d2013-05-07 06:12:01 +000072 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass94e5fa42013-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 Glassbbb467d2013-05-07 06:12:01 +000076 node_name, image_name);
Simon Glass1152a052016-07-03 09:40:44 -060077 return -ENOENT;
Simon Glass94e5fa42013-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 Glassbbb467d2013-05-07 06:12:01 +000082 algo, node_name, image_name);
Simon Glass1152a052016-07-03 09:40:44 -060083 return -EPROTONOSUPPORT;
Simon Glass94e5fa42013-05-07 06:11:55 +000084 }
85
Simon Glass1152a052016-07-03 09:40:44 -060086 ret = fit_set_hash_value(fit, noffset, value, value_len);
87 if (ret) {
Simon Glass94e5fa42013-05-07 06:11:55 +000088 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
Simon Glassbbb467d2013-05-07 06:12:01 +000089 node_name, image_name);
Simon Glass1152a052016-07-03 09:40:44 -060090 return ret;
Simon Glass94e5fa42013-05-07 06:11:55 +000091 }
92
93 return 0;
94}
95
96/**
Simon Glass56518e72013-06-13 15:10:01 -070097 * fit_image_write_sig() - write the signature to a FIT
Simon Glass604f23d2013-05-07 06:11:54 +000098 *
Simon Glass56518e72013-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 Kiszka5902a392022-01-14 10:21:19 +0100113 int region_proplen, const char *cmdname, const char *algo_name)
Simon Glass56518e72013-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 Kiernan795f4522018-06-20 20:10:52 +0000135 if (!ret) {
136 time_t timestamp = imagetool_get_source_date(cmdname,
137 time(NULL));
Ming Liu7c397992021-05-31 09:04:51 +0200138 uint32_t t = cpu_to_uimage(timestamp);
Alex Kiernan795f4522018-06-20 20:10:52 +0000139
Ming Liu7c397992021-05-31 09:04:51 +0200140 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
141 sizeof(uint32_t));
Alex Kiernan795f4522018-06-20 20:10:52 +0000142 }
Simon Glass56518e72013-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 Reed7346c1e2018-06-09 11:45:20 -0400148 /* This is a legacy offset, it is unused, and must remain 0. */
Simon Glass56518e72013-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 Kiszka5902a392022-01-14 10:21:19 +0100156 if (algo_name && !ret)
157 ret = fdt_setprop_string(fit, noffset, "algo", algo_name);
Simon Glass56518e72013-06-13 15:10:01 -0700158
159 return ret;
160}
161
162static int fit_image_setup_sig(struct image_sign_info *info,
Alexandru Gagniuc36bfcb62021-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 Kiszka5902a392022-01-14 10:21:19 +0100165 const char *engine_id, const char *algo_name)
Simon Glass56518e72013-06-13 15:10:01 -0700166{
167 const char *node_name;
Philippe Reynes20031562018-11-14 13:51:00 +0100168 const char *padding_name;
Simon Glass56518e72013-06-13 15:10:01 -0700169
170 node_name = fit_get_name(fit, noffset, NULL);
Jan Kiszka5902a392022-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 Glass56518e72013-06-13 15:10:01 -0700177 }
178
Philippe Reynes20031562018-11-14 13:51:00 +0100179 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
180
Simon Glass56518e72013-06-13 15:10:01 -0700181 memset(info, '\0', sizeof(*info));
182 info->keydir = keydir;
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600183 info->keyfile = keyfile;
Simon Glass72188f52020-03-18 11:44:06 -0600184 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700185 info->fit = fit;
186 info->node_offset = noffset;
Masahiro Yamada1d88a992017-10-27 13:25:21 +0900187 info->name = strdup(algo_name);
Andrew Duda83dd98e2016-11-08 18:53:41 +0000188 info->checksum = image_get_checksum_algo(algo_name);
189 info->crypto = image_get_crypto_algo(algo_name);
Philippe Reynes20031562018-11-14 13:51:00 +0100190 info->padding = image_get_padding_algo(padding_name);
Simon Glass56518e72013-06-13 15:10:01 -0700191 info->require_keys = require_keys;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600192 info->engine_id = engine_id;
Andrew Duda83dd98e2016-11-08 18:53:41 +0000193 if (!info->checksum || !info->crypto) {
Simon Glass56518e72013-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 Glass70e6bcc2021-11-12 12:28:06 -0700206 * generate a signed hash of the supplied data and store it in the node.
Simon Glass56518e72013-06-13 15:10:01 -0700207 *
208 * @keydir: Directory containing keys to use for signing
Simon Glass70e6bcc2021-11-12 12:28:06 -0700209 * @keydest: Destination FDT blob to write public keys into (NULL if none)
Simon Glass56518e72013-06-13 15:10:01 -0700210 * @fit: pointer to the FIT format image header
Simon Glass70e6bcc2021-11-12 12:28:06 -0700211 * @image_name: name of image being processed (used to display errors)
Simon Glass56518e72013-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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600217 * @engine_id: Engine to use for signing
Simon Glass9737c2d2021-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 Glass56518e72013-06-13 15:10:01 -0700220 */
Alexandru Gagniuc36bfcb62021-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 Glass56518e72013-06-13 15:10:01 -0700223 int noffset, const void *data, size_t size,
Alex Kiernan795f4522018-06-20 20:10:52 +0000224 const char *comment, int require_keys, const char *engine_id,
Jan Kiszka5902a392022-01-14 10:21:19 +0100225 const char *cmdname, const char *algo_name)
Simon Glass56518e72013-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 Gagniuc36bfcb62021-02-19 12:45:17 -0600234 if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
235 noffset, require_keys ? "image" : NULL,
Jan Kiszka5902a392022-01-14 10:21:19 +0100236 engine_id, algo_name))
Simon Glass56518e72013-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 Duda83dd98e2016-11-08 18:53:41 +0000242 ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
Simon Glass56518e72013-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 Kiszka5902a392022-01-14 10:21:19 +0100254 NULL, 0, cmdname, algo_name);
Simon Glass56518e72013-06-13 15:10:01 -0700255 if (ret) {
Simon Glassa9468112014-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 Glass56518e72013-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 Glass72188f52020-03-18 11:44:06 -0600265 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700266
mario.six@gdsys.cc713fb2d2016-07-22 08:58:40 +0200267 /*
268 * Write the public key into the supplied FDT file; this might fail
mario.six@gdsys.ccc236ebd2016-07-19 11:07:06 +0200269 * several times, since we try signing with successively increasing
mario.six@gdsys.cc713fb2d2016-07-22 08:58:40 +0200270 * size values
271 */
Masahiro Yamada6793d012017-10-27 15:04:20 +0900272 if (keydest) {
273 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glassc033dc82021-11-12 12:28:11 -0700274 if (ret < 0) {
Masahiro Yamada6793d012017-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 Glass9737c2d2021-11-12 12:28:12 -0700279 /* Return the node that was written to */
280 return ret;
Masahiro Yamada6793d012017-10-27 15:04:20 +0900281 }
Simon Glass56518e72013-06-13 15:10:01 -0700282
283 return 0;
284}
285
Philippe Reynes7298e422019-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 Schuchardt3311eda2020-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 Reynes7298e422019-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 Cascadian2c6bcab2021-09-28 10:11:46 -0700325 printf("Can't read all file %s (read %zd bytes, expected %lld)\n",
Heinrich Schuchardt3311eda2020-10-08 20:51:24 +0200326 filename, n, (long long)sbuf.st_size);
Philippe Reynes7298e422019-12-18 18:25:41 +0100327 goto err;
328 }
329
330 ret = 0;
331
332err:
333 close(fd);
334 return ret;
335}
336
Philippe Reynesa6982a62020-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 Glass7f0f4e12021-05-13 19:39:20 -0600341 int i, ret;
Philippe Reynesa6982a62020-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 Glass7f0f4e12021-05-13 19:39:20 -0600350 if (ret) {
351 printf("%s: clock_gettime has failed (%s)\n", __func__,
352 strerror(errno));
Philippe Reynesa6982a62020-09-17 15:01:46 +0200353 goto out;
354 }
355
Philippe Reynescc34f042020-11-13 16:37:46 +0100356 srandom(date.tv_nsec);
Philippe Reynesa6982a62020-09-17 15:01:46 +0200357
358 for (i = 0; i < size; i++) {
Philippe Reynescc34f042020-11-13 16:37:46 +0100359 *tmp = random() & 0xff;
Philippe Reynesa6982a62020-09-17 15:01:46 +0200360 tmp++;
361 }
362
363 out:
364 return ret;
365}
366
Philippe Reynes7298e422019-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 Oppenlanderc5202662020-07-30 14:22:13 +1000370 int noffset)
Philippe Reynes7298e422019-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 Oppenlanderc5202662020-07-30 14:22:13 +1000377 printf("Can't get algo name for cipher in image '%s'\n",
378 image_name);
Philippe Reynes7298e422019-12-18 18:25:41 +0100379 goto out;
380 }
381
382 info->keydir = keydir;
383
384 /* Read the key name */
Simon Glass72188f52020-03-18 11:44:06 -0600385 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Philippe Reynes7298e422019-12-18 18:25:41 +0100386 if (!info->keyname) {
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000387 printf("Can't get key name for cipher in image '%s'\n",
388 image_name);
Philippe Reynes7298e422019-12-18 18:25:41 +0100389 goto out;
390 }
391
Philippe Reynesa6982a62020-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 Reynes7298e422019-12-18 18:25:41 +0100398 info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
Philippe Reynes7298e422019-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 Reynes7298e422019-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 Reynesa6982a62020-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 Reynes7298e422019-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 Oppenlander04aeebb2020-07-30 14:22:14 +1000452 /* Replace data with ciphered data */
Philippe Reynes7298e422019-12-18 18:25:41 +0100453 ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
454 data_ciphered, data_ciphered_len);
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000455 if (ret == -FDT_ERR_NOSPACE) {
456 ret = -ENOSPC;
457 goto out;
458 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100459 if (ret) {
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000460 printf("Can't replace data with ciphered data (err = %d)\n", ret);
Philippe Reynes7298e422019-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 Oppenlander04aeebb2020-07-30 14:22:14 +1000466 if (ret == -FDT_ERR_NOSPACE) {
467 ret = -ENOSPC;
468 goto out;
469 }
Philippe Reynes7298e422019-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 Oppenlanderc5202662020-07-30 14:22:13 +1000482 int node_noffset, const void *data, size_t size,
Philippe Reynes7298e422019-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 Oppenlanderc5202662020-07-30 14:22:13 +1000493 image_noffset, node_noffset);
Philippe Reynes7298e422019-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 Reynesa6982a62020-09-17 15:01:46 +0200506 * And, if needed, write the iv in the FIT file
Philippe Reynes7298e422019-12-18 18:25:41 +0100507 */
508 if (keydest) {
Philippe Reynesa6982a62020-09-17 15:01:46 +0200509 ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
Philippe Reynes7298e422019-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 Oppenlanderb33e5cc2020-07-30 14:22:15 +1000536 int cipher_node_offset, len;
Philippe Reynes7298e422019-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 Oppenlanderb33e5cc2020-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 Reynes7298e422019-12-18 18:25:41 +0100564
Patrick Oppenlanderc5202662020-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 Reynes7298e422019-12-18 18:25:41 +0100573 }
Patrick Oppenlanderc5202662020-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 Reynes7298e422019-12-18 18:25:41 +0100578}
579
Simon Glass56518e72013-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 Glassbbb467d2013-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 Glass604f23d2013-05-07 06:11:54 +0000588 *
589 * Input component image node structure:
590 *
Andre Przywarab2267e82017-12-04 02:05:10 +0000591 * o image-1 (at image_noffset)
Simon Glass604f23d2013-05-07 06:11:54 +0000592 * | - data = [binary data]
Andre Przywarab2267e82017-12-04 02:05:10 +0000593 * o hash-1
Simon Glass604f23d2013-05-07 06:11:54 +0000594 * |- algo = "sha1"
595 *
596 * Output component image node structure:
597 *
Andre Przywarab2267e82017-12-04 02:05:10 +0000598 * o image-1 (at image_noffset)
Simon Glass604f23d2013-05-07 06:11:54 +0000599 * | - data = [binary data]
Andre Przywarab2267e82017-12-04 02:05:10 +0000600 * o hash-1
Simon Glass604f23d2013-05-07 06:11:54 +0000601 * |- algo = "sha1"
602 * |- value = sha1(data)
603 *
Simon Glassbbb467d2013-05-07 06:12:01 +0000604 * For signature details, please see doc/uImage.FIT/signature.txt
605 *
Simon Glass56518e72013-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 Glassbbb467d2013-05-07 06:12:01 +0000608 * @fit: Pointer to the FIT format image header
609 * @image_noffset: Requested component image node
Simon Glass56518e72013-06-13 15:10:01 -0700610 * @comment: Comment to add to signature nodes
611 * @require_keys: Mark all keys as 'required'
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600612 * @engine_id: Engine to use for signing
Simon Glassbbb467d2013-05-07 06:12:01 +0000613 * @return: 0 on success, <0 on failure
Simon Glass604f23d2013-05-07 06:11:54 +0000614 */
Alexandru Gagniuc36bfcb62021-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 Kiszka5902a392022-01-14 10:21:19 +0100618 const char *cmdname, const char* algo_name)
Simon Glass604f23d2013-05-07 06:11:54 +0000619{
Simon Glassbbb467d2013-05-07 06:12:01 +0000620 const char *image_name;
Simon Glass604f23d2013-05-07 06:11:54 +0000621 const void *data;
622 size_t size;
Simon Glass604f23d2013-05-07 06:11:54 +0000623 int noffset;
Simon Glass604f23d2013-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 Glass94e5fa42013-05-07 06:11:55 +0000631 image_name = fit_get_name(fit, image_noffset, NULL);
632
Simon Glass604f23d2013-05-07 06:11:54 +0000633 /* Process all hash subnodes of the component image node */
Simon Glassbbb467d2013-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 Przywarab2267e82017-12-04 02:05:10 +0000643 * names, e.g. hash-1, hash-2, signature-1, etc.
Simon Glassbbb467d2013-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 Gagniuc36bfcb62021-02-19 12:45:17 -0600650 } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) &&
Simon Glass56518e72013-06-13 15:10:01 -0700651 !strncmp(node_name, FIT_SIG_NODENAME,
652 strlen(FIT_SIG_NODENAME))) {
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600653 ret = fit_image_process_sig(keydir, keyfile, keydest,
Simon Glass56518e72013-06-13 15:10:01 -0700654 fit, image_name, noffset, data, size,
Jan Kiszka5902a392022-01-14 10:21:19 +0100655 comment, require_keys, engine_id, cmdname,
656 algo_name);
Simon Glass604f23d2013-05-07 06:11:54 +0000657 }
Simon Glass9737c2d2021-11-12 12:28:12 -0700658 if (ret < 0)
Simon Glass1152a052016-07-03 09:40:44 -0600659 return ret;
Simon Glassbbb467d2013-05-07 06:12:01 +0000660 }
661
662 return 0;
663}
664
Simon Glass4d098522013-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 Glass70e6bcc2021-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 Glass4d098522013-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 Glass70e6bcc2021-11-12 12:28:06 -0700705 /* If there is an "sign-image" property, use that */
Simon Glass4d098522013-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 Glass70e6bcc2021-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 Reynes5a4116f2020-11-24 14:39:47 +0100737{
Simon Glass48422342021-11-12 12:28:07 -0700738 char path[200];
Philippe Reynes5a4116f2020-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 Reynes5a4116f2020-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 Glass70e6bcc2021-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 Glass4d098522013-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 Reynes5a4116f2020-11-24 14:39:47 +0100823 char name[200];
Simon Glass4d098522013-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 Schocher66b36f82014-03-03 12:19:23 +0100841 prop = fit_config_get_image_list(fit, sig_offset, &len,
Simon Glass4d098522013-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 Glass4d098522013-06-13 15:10:09 -0700850 int image_noffset;
Philippe Reynesedfeba72020-11-24 14:39:48 +0100851 int index, max_index;
Simon Glass4d098522013-06-13 15:10:09 -0700852
Philippe Reynesedfeba72020-11-24 14:39:48 +0100853 max_index = fdt_stringlist_count(fit, conf_noffset, iname);
Simon Glass4d098522013-06-13 15:10:09 -0700854
Philippe Reynesedfeba72020-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;
864
865 return -ENOENT;
866 }
867
Simon Glass70e6bcc2021-11-12 12:28:06 -0700868 ret = fit_config_add_hash(fit, image_noffset, node_inc,
869 conf_name, sig_name, iname);
Philippe Reynesedfeba72020-11-24 14:39:48 +0100870 if (ret < 0)
871 return ret;
872
873 image_count++;
Simon Glass4d098522013-06-13 15:10:09 -0700874 }
Simon Glass4d098522013-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 Glass4d098522013-06-13 15:10:09 -0700889}
890
Simon Glass70e6bcc2021-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 Glass4d098522013-06-13 15:10:09 -0700917{
Sean Anderson0abe3322022-10-20 15:41:10 -0400918 char * const exc_prop[] = {
919 FIT_DATA_PROP,
920 FIT_DATA_SIZE_PROP,
921 FIT_DATA_POSITION_PROP,
922 FIT_DATA_OFFSET_PROP,
923 };
Simon Glass4d098522013-06-13 15:10:09 -0700924 struct strlist node_inc;
925 struct image_region *region;
926 struct fdt_region fdt_regions[100];
927 const char *conf_name, *sig_name;
928 char path[200];
929 int count, i;
930 char *region_prop;
931 int ret, len;
932
933 conf_name = fit_get_name(fit, conf_noffset, NULL);
Simon Glass70e6bcc2021-11-12 12:28:06 -0700934 sig_name = fit_get_name(fit, sig_offset, NULL);
Simon Glass4d098522013-06-13 15:10:09 -0700935 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
936
937 /* Get a list of nodes we want to hash */
Simon Glass70e6bcc2021-11-12 12:28:06 -0700938 ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset,
939 &node_inc);
Simon Glass4d098522013-06-13 15:10:09 -0700940 if (ret)
941 return ret;
942
943 /* Get a list of regions to hash */
944 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
945 exc_prop, ARRAY_SIZE(exc_prop),
946 fdt_regions, ARRAY_SIZE(fdt_regions),
947 path, sizeof(path), 1);
948 if (count < 0) {
949 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
950 sig_name, fdt_strerror(ret));
951 return -EIO;
952 }
953 if (count == 0) {
954 printf("No data to hash for configuration '%s/%s': %s\n",
955 conf_name, sig_name, fdt_strerror(ret));
956 return -EINVAL;
957 }
958
959 /* Build our list of data blocks */
960 region = fit_region_make_list(fit, fdt_regions, count, NULL);
961 if (!region) {
962 printf("Out of memory hashing configuration '%s/%s'\n",
963 conf_name, sig_name);
964 return -ENOMEM;
965 }
966
967 /* Create a list of all hashed properties */
968 debug("Hash nodes:\n");
969 for (i = len = 0; i < node_inc.count; i++) {
970 debug(" %s\n", node_inc.strings[i]);
971 len += strlen(node_inc.strings[i]) + 1;
972 }
973 region_prop = malloc(len);
974 if (!region_prop) {
975 printf("Out of memory setting up regions for configuration '%s/%s'\n",
976 conf_name, sig_name);
977 return -ENOMEM;
978 }
979 for (i = len = 0; i < node_inc.count;
980 len += strlen(node_inc.strings[i]) + 1, i++)
981 strcpy(region_prop + len, node_inc.strings[i]);
982 strlist_free(&node_inc);
983
984 *region_countp = count;
985 *regionp = region;
986 *region_propp = region_prop;
987 *region_proplen = len;
988
989 return 0;
990}
991
Simon Glass9737c2d2021-11-12 12:28:12 -0700992/**
993 * fit_config_process_sig - Process a single subnode of the configurations/ node
994 *
995 * Generate a signed hash of the supplied data and store it in the node.
996 *
997 * @keydir: Directory containing keys to use for signing
998 * @keydest: Destination FDT blob to write public keys into (NULL if none)
999 * @fit: pointer to the FIT format image header
1000 * @conf_name name of config being processed (used to display errors)
1001 * @conf_noffset: Offset of configuration node, e.g. '/configurations/conf-1'
1002 * @noffset: subnode offset, e.g. '/configurations/conf-1/sig-1'
1003 * @comment: Comment to add to signature nodes
1004 * @require_keys: Mark all keys as 'required'
1005 * @engine_id: Engine to use for signing
1006 * @cmdname: Command name used when reporting errors
1007 * @return keydest node if @keydest is non-NULL, else 0 if none; -ve error code
1008 * on failure
1009 */
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001010static int fit_config_process_sig(const char *keydir, const char *keyfile,
Simon Glass70e6bcc2021-11-12 12:28:06 -07001011 void *keydest, void *fit, const char *conf_name,
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001012 int conf_noffset, int noffset, const char *comment,
Jan Kiszka5902a392022-01-14 10:21:19 +01001013 int require_keys, const char *engine_id, const char *cmdname,
1014 const char *algo_name)
Simon Glass4d098522013-06-13 15:10:09 -07001015{
1016 struct image_sign_info info;
1017 const char *node_name;
1018 struct image_region *region;
1019 char *region_prop;
1020 int region_proplen;
1021 int region_count;
1022 uint8_t *value;
1023 uint value_len;
1024 int ret;
1025
1026 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass70e6bcc2021-11-12 12:28:06 -07001027 if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
1028 &region_count, &region_prop,
1029 &region_proplen))
Simon Glass4d098522013-06-13 15:10:09 -07001030 return -1;
1031
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001032 if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
Jan Kiszka5902a392022-01-14 10:21:19 +01001033 require_keys ? "conf" : NULL, engine_id,
1034 algo_name))
Simon Glass4d098522013-06-13 15:10:09 -07001035 return -1;
1036
Andrew Duda83dd98e2016-11-08 18:53:41 +00001037 ret = info.crypto->sign(&info, region, region_count, &value,
1038 &value_len);
Simon Glass4d098522013-06-13 15:10:09 -07001039 free(region);
1040 if (ret) {
1041 printf("Failed to sign '%s' signature node in '%s' conf node\n",
1042 node_name, conf_name);
1043
1044 /* We allow keys to be missing */
1045 if (ret == -ENOENT)
1046 return 0;
1047 return -1;
1048 }
1049
Simon Glassa9468112014-06-02 22:04:53 -06001050 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Jan Kiszka5902a392022-01-14 10:21:19 +01001051 region_prop, region_proplen, cmdname,
1052 algo_name);
Simon Glassa9468112014-06-02 22:04:53 -06001053 if (ret) {
1054 if (ret == -FDT_ERR_NOSPACE)
1055 return -ENOSPC;
1056 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
1057 node_name, conf_name, fdt_strerror(ret));
Simon Glass4d098522013-06-13 15:10:09 -07001058 return -1;
1059 }
1060 free(value);
1061 free(region_prop);
1062
1063 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glass72188f52020-03-18 11:44:06 -06001064 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass4d098522013-06-13 15:10:09 -07001065
1066 /* Write the public key into the supplied FDT file */
Simon Glassa9468112014-06-02 22:04:53 -06001067 if (keydest) {
Andrew Duda83dd98e2016-11-08 18:53:41 +00001068 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glassc033dc82021-11-12 12:28:11 -07001069 if (ret < 0) {
Masahiro Yamada76b9cba2017-10-27 15:04:21 +09001070 printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
Simon Glassa9468112014-06-02 22:04:53 -06001071 node_name, conf_name);
Simon Glassa9468112014-06-02 22:04:53 -06001072 }
Simon Glass9737c2d2021-11-12 12:28:12 -07001073 return ret;
Simon Glass4d098522013-06-13 15:10:09 -07001074 }
1075
1076 return 0;
1077}
1078
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001079static int fit_config_add_verification_data(const char *keydir,
1080 const char *keyfile, void *keydest, void *fit, int conf_noffset,
1081 const char *comment, int require_keys, const char *engine_id,
Simon Glass2d2384b2021-11-12 12:28:13 -07001082 const char *cmdname, const char *algo_name,
1083 struct image_summary *summary)
Simon Glass4d098522013-06-13 15:10:09 -07001084{
1085 const char *conf_name;
1086 int noffset;
1087
1088 conf_name = fit_get_name(fit, conf_noffset, NULL);
1089
1090 /* Process all hash subnodes of the configuration node */
1091 for (noffset = fdt_first_subnode(fit, conf_noffset);
1092 noffset >= 0;
1093 noffset = fdt_next_subnode(fit, noffset)) {
1094 const char *node_name;
1095 int ret = 0;
1096
1097 node_name = fit_get_name(fit, noffset, NULL);
1098 if (!strncmp(node_name, FIT_SIG_NODENAME,
1099 strlen(FIT_SIG_NODENAME))) {
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001100 ret = fit_config_process_sig(keydir, keyfile, keydest,
Simon Glass4d098522013-06-13 15:10:09 -07001101 fit, conf_name, conf_noffset, noffset, comment,
Jan Kiszka5902a392022-01-14 10:21:19 +01001102 require_keys, engine_id, cmdname, algo_name);
Simon Glass2d2384b2021-11-12 12:28:13 -07001103 if (ret < 0)
1104 return ret;
1105
1106 summary->sig_offset = noffset;
1107 fdt_get_path(fit, noffset, summary->sig_path,
1108 sizeof(summary->sig_path));
1109
1110 if (keydest) {
1111 summary->keydest_offset = ret;
1112 fdt_get_path(keydest, ret,
1113 summary->keydest_path,
1114 sizeof(summary->keydest_path));
1115 }
Simon Glass4d098522013-06-13 15:10:09 -07001116 }
Simon Glass4d098522013-06-13 15:10:09 -07001117 }
1118
1119 return 0;
1120}
1121
Philippe Reynes6e052d12022-03-28 22:57:02 +02001122/*
1123 * 0) open file (open)
1124 * 1) read certificate (PEM_read_X509)
1125 * 2) get public key (X509_get_pubkey)
1126 * 3) provide der format (d2i_RSAPublicKey)
1127 */
1128static int read_pub_key(const char *keydir, const void *name,
1129 unsigned char **pubkey, int *pubkey_len)
1130{
1131 char path[1024];
1132 EVP_PKEY *key = NULL;
1133 X509 *cert;
1134 FILE *f;
1135 int ret;
1136
1137 memset(path, 0, 1024);
1138 snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name);
1139
1140 /* Open certificate file */
1141 f = fopen(path, "r");
1142 if (!f) {
1143 fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
1144 path, strerror(errno));
1145 return -EACCES;
1146 }
1147
1148 /* Read the certificate */
1149 cert = NULL;
1150 if (!PEM_read_X509(f, &cert, NULL, NULL)) {
1151 printf("Couldn't read certificate");
1152 ret = -EINVAL;
1153 goto err_cert;
1154 }
1155
1156 /* Get the public key from the certificate. */
1157 key = X509_get_pubkey(cert);
1158 if (!key) {
1159 printf("Couldn't read public key\n");
1160 ret = -EINVAL;
1161 goto err_pubkey;
1162 }
1163
1164 /* Get DER form */
1165 ret = i2d_PublicKey(key, pubkey);
1166 if (ret < 0) {
1167 printf("Couldn't get DER form\n");
1168 ret = -EINVAL;
1169 goto err_pubkey;
1170 }
1171
1172 *pubkey_len = ret;
1173 ret = 0;
1174
1175err_pubkey:
1176 X509_free(cert);
1177err_cert:
1178 fclose(f);
1179 return ret;
1180}
1181
1182int fit_pre_load_data(const char *keydir, void *keydest, void *fit)
1183{
1184 int pre_load_noffset;
1185 const void *algo_name;
1186 const void *key_name;
1187 unsigned char *pubkey = NULL;
1188 int ret, pubkey_len;
1189
1190 if (!keydir || !keydest || !fit)
1191 return 0;
1192
1193 /* Search node pre-load sig */
1194 pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH);
1195 if (pre_load_noffset < 0) {
1196 ret = 0;
1197 goto out;
1198 }
1199
1200 algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL);
1201 key_name = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL);
1202
1203 /* Check that all mandatory properties are present */
1204 if (!algo_name || !key_name) {
1205 if (!algo_name)
1206 printf("The property algo-name is missing in the node %s\n",
1207 IMAGE_PRE_LOAD_PATH);
1208 if (!key_name)
1209 printf("The property key-name is missing in the node %s\n",
1210 IMAGE_PRE_LOAD_PATH);
Mark Kettenis61657182022-04-26 19:24:38 +02001211 ret = -EINVAL;
Philippe Reynes6e052d12022-03-28 22:57:02 +02001212 goto out;
1213 }
1214
1215 /* Read public key */
1216 ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len);
1217 if (ret < 0)
1218 goto out;
1219
1220 /* Add the public key to the device tree */
1221 ret = fdt_setprop(keydest, pre_load_noffset, "public-key",
1222 pubkey, pubkey_len);
1223 if (ret)
1224 printf("Can't set public-key in node %s (ret = %d)\n",
1225 IMAGE_PRE_LOAD_PATH, ret);
1226
1227 out:
1228 return ret;
1229}
1230
Philippe Reynes7298e422019-12-18 18:25:41 +01001231int fit_cipher_data(const char *keydir, void *keydest, void *fit,
1232 const char *comment, int require_keys,
1233 const char *engine_id, const char *cmdname)
1234{
1235 int images_noffset;
1236 int noffset;
1237 int ret;
1238
1239 /* Find images parent node offset */
1240 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1241 if (images_noffset < 0) {
1242 printf("Can't find images parent node '%s' (%s)\n",
1243 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1244 return images_noffset;
1245 }
1246
1247 /* Process its subnodes, print out component images details */
1248 for (noffset = fdt_first_subnode(fit, images_noffset);
1249 noffset >= 0;
1250 noffset = fdt_next_subnode(fit, noffset)) {
1251 /*
1252 * Direct child node of the images parent node,
1253 * i.e. component image node.
1254 */
1255 ret = fit_image_cipher_data(keydir, keydest,
1256 fit, noffset, comment,
1257 require_keys, engine_id,
1258 cmdname);
1259 if (ret)
1260 return ret;
1261 }
1262
1263 return 0;
1264}
1265
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001266int fit_add_verification_data(const char *keydir, const char *keyfile,
1267 void *keydest, void *fit, const char *comment,
1268 int require_keys, const char *engine_id,
Simon Glass2d2384b2021-11-12 12:28:13 -07001269 const char *cmdname, const char *algo_name,
1270 struct image_summary *summary)
Simon Glassbbb467d2013-05-07 06:12:01 +00001271{
Simon Glass4d098522013-06-13 15:10:09 -07001272 int images_noffset, confs_noffset;
Simon Glassbbb467d2013-05-07 06:12:01 +00001273 int noffset;
1274 int ret;
1275
1276 /* Find images parent node offset */
1277 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1278 if (images_noffset < 0) {
1279 printf("Can't find images parent node '%s' (%s)\n",
1280 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1281 return images_noffset;
1282 }
1283
1284 /* Process its subnodes, print out component images details */
1285 for (noffset = fdt_first_subnode(fit, images_noffset);
1286 noffset >= 0;
1287 noffset = fdt_next_subnode(fit, noffset)) {
1288 /*
1289 * Direct child node of the images parent node,
1290 * i.e. component image node.
1291 */
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001292 ret = fit_image_add_verification_data(keydir, keyfile, keydest,
Alex Kiernan795f4522018-06-20 20:10:52 +00001293 fit, noffset, comment, require_keys, engine_id,
Jan Kiszka5902a392022-01-14 10:21:19 +01001294 cmdname, algo_name);
Simon Glass90cfae22022-12-21 16:08:23 -07001295 if (ret) {
1296 printf("Can't add verification data for node '%s' (%s)\n",
1297 fdt_get_name(fit, noffset, NULL),
1298 fdt_strerror(ret));
Simon Glassbbb467d2013-05-07 06:12:01 +00001299 return ret;
Simon Glass90cfae22022-12-21 16:08:23 -07001300 }
Simon Glass604f23d2013-05-07 06:11:54 +00001301 }
1302
Simon Glass4d098522013-06-13 15:10:09 -07001303 /* If there are no keys, we can't sign configurations */
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001304 if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile))
Simon Glass4d098522013-06-13 15:10:09 -07001305 return 0;
1306
1307 /* Find configurations parent node offset */
1308 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1309 if (confs_noffset < 0) {
1310 printf("Can't find images parent node '%s' (%s)\n",
Heiko Schocher04a710a2014-08-11 11:02:17 +02001311 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Simon Glass4d098522013-06-13 15:10:09 -07001312 return -ENOENT;
1313 }
1314
1315 /* Process its subnodes, print out component images details */
1316 for (noffset = fdt_first_subnode(fit, confs_noffset);
1317 noffset >= 0;
1318 noffset = fdt_next_subnode(fit, noffset)) {
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001319 ret = fit_config_add_verification_data(keydir, keyfile, keydest,
Simon Glass4d098522013-06-13 15:10:09 -07001320 fit, noffset, comment,
George McCollisterf1ca1fd2017-01-06 13:14:17 -06001321 require_keys,
Jan Kiszka5902a392022-01-14 10:21:19 +01001322 engine_id, cmdname,
Simon Glass2d2384b2021-11-12 12:28:13 -07001323 algo_name, summary);
Simon Glass4d098522013-06-13 15:10:09 -07001324 if (ret)
1325 return ret;
1326 }
1327
Simon Glass604f23d2013-05-07 06:11:54 +00001328 return 0;
1329}
Heiko Schocher29a23f92014-03-03 12:19:30 +01001330
1331#ifdef CONFIG_FIT_SIGNATURE
Simon Glassc3aa81e2020-03-18 11:44:03 -06001332int fit_check_sign(const void *fit, const void *key,
1333 const char *fit_uname_config)
Heiko Schocher29a23f92014-03-03 12:19:30 +01001334{
1335 int cfg_noffset;
1336 int ret;
1337
Simon Glassc3aa81e2020-03-18 11:44:03 -06001338 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
Heiko Schocher29a23f92014-03-03 12:19:30 +01001339 if (!cfg_noffset)
1340 return -1;
1341
Simon Glass382cf622020-03-18 11:43:56 -06001342 printf("Verifying Hash Integrity for node '%s'... ",
1343 fdt_get_name(fit, cfg_noffset, NULL));
Simon Glassce1400f2014-06-12 07:24:53 -06001344 ret = fit_config_verify(fit, cfg_noffset);
1345 if (ret)
1346 return ret;
Simon Glassc3aa81e2020-03-18 11:44:03 -06001347 printf("Verified OK, loading images\n");
Simon Glassce1400f2014-06-12 07:24:53 -06001348 ret = bootm_host_load_images(fit, cfg_noffset);
1349
Heiko Schocher29a23f92014-03-03 12:19:30 +01001350 return ret;
1351}
1352#endif