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