Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 604f23d | 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 | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "mkimage.h" |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 12 | #include <bootm.h> |
Masahiro Yamada | 64045a6 | 2020-04-16 18:30:18 +0900 | [diff] [blame] | 13 | #include <fdt_region.h> |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 14 | #include <image.h> |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 15 | #include <version.h> |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 16 | |
| 17 | /** |
Simon Glass | b726091 | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 18 | * fit_set_hash_value - set hash value in requested has node |
| 19 | * @fit: pointer to the FIT format image header |
| 20 | * @noffset: hash node offset |
| 21 | * @value: hash value to be set |
| 22 | * @value_len: hash value length |
| 23 | * |
| 24 | * fit_set_hash_value() attempts to set hash value in a node at offset |
| 25 | * given and returns operation status to the caller. |
| 26 | * |
| 27 | * returns |
| 28 | * 0, on success |
| 29 | * -1, on failure |
| 30 | */ |
| 31 | static int fit_set_hash_value(void *fit, int noffset, uint8_t *value, |
| 32 | int value_len) |
| 33 | { |
| 34 | int ret; |
| 35 | |
| 36 | ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); |
| 37 | if (ret) { |
| 38 | printf("Can't set hash '%s' property for '%s' node(%s)\n", |
| 39 | FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL), |
| 40 | fdt_strerror(ret)); |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 41 | return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
Simon Glass | b726091 | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | /** |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 48 | * fit_image_process_hash - Process a single subnode of the images/ node |
| 49 | * |
| 50 | * Check each subnode and process accordingly. For hash nodes we generate |
| 51 | * a hash of the supplised data and store it in the node. |
| 52 | * |
| 53 | * @fit: pointer to the FIT format image header |
| 54 | * @image_name: name of image being processes (used to display errors) |
| 55 | * @noffset: subnode offset |
| 56 | * @data: data to process |
| 57 | * @size: size of data in bytes |
| 58 | * @return 0 if ok, -1 on error |
| 59 | */ |
| 60 | static int fit_image_process_hash(void *fit, const char *image_name, |
| 61 | int noffset, const void *data, size_t size) |
| 62 | { |
| 63 | uint8_t value[FIT_MAX_HASH_LEN]; |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 64 | const char *node_name; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 65 | int value_len; |
| 66 | char *algo; |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 67 | int ret; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 68 | |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 69 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 70 | |
| 71 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
| 72 | printf("Can't get hash algo property for '%s' hash node in '%s' image node\n", |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 73 | node_name, image_name); |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 74 | return -ENOENT; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (calculate_hash(data, size, algo, value, &value_len)) { |
| 78 | printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n", |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 79 | algo, node_name, image_name); |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 80 | return -EPROTONOSUPPORT; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 83 | ret = fit_set_hash_value(fit, noffset, value, value_len); |
| 84 | if (ret) { |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 85 | printf("Can't set hash value for '%s' hash node in '%s' image node\n", |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 86 | node_name, image_name); |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 87 | return ret; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /** |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 94 | * fit_image_write_sig() - write the signature to a FIT |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 95 | * |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 96 | * This writes the signature and signer data to the FIT. |
| 97 | * |
| 98 | * @fit: pointer to the FIT format image header |
| 99 | * @noffset: hash node offset |
| 100 | * @value: signature value to be set |
| 101 | * @value_len: signature value length |
| 102 | * @comment: Text comment to write (NULL for none) |
| 103 | * |
| 104 | * returns |
| 105 | * 0, on success |
| 106 | * -FDT_ERR_..., on failure |
| 107 | */ |
| 108 | static int fit_image_write_sig(void *fit, int noffset, uint8_t *value, |
| 109 | int value_len, const char *comment, const char *region_prop, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 110 | int region_proplen, const char *cmdname) |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 111 | { |
| 112 | int string_size; |
| 113 | int ret; |
| 114 | |
| 115 | /* |
| 116 | * Get the current string size, before we update the FIT and add |
| 117 | * more |
| 118 | */ |
| 119 | string_size = fdt_size_dt_strings(fit); |
| 120 | |
| 121 | ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); |
| 122 | if (!ret) { |
| 123 | ret = fdt_setprop_string(fit, noffset, "signer-name", |
| 124 | "mkimage"); |
| 125 | } |
| 126 | if (!ret) { |
| 127 | ret = fdt_setprop_string(fit, noffset, "signer-version", |
| 128 | PLAIN_VERSION); |
| 129 | } |
| 130 | if (comment && !ret) |
| 131 | ret = fdt_setprop_string(fit, noffset, "comment", comment); |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 132 | if (!ret) { |
| 133 | time_t timestamp = imagetool_get_source_date(cmdname, |
| 134 | time(NULL)); |
| 135 | |
| 136 | ret = fit_set_timestamp(fit, noffset, timestamp); |
| 137 | } |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 138 | if (region_prop && !ret) { |
| 139 | uint32_t strdata[2]; |
| 140 | |
| 141 | ret = fdt_setprop(fit, noffset, "hashed-nodes", |
| 142 | region_prop, region_proplen); |
Teddy Reed | 7346c1e | 2018-06-09 11:45:20 -0400 | [diff] [blame] | 143 | /* This is a legacy offset, it is unused, and must remain 0. */ |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 144 | strdata[0] = 0; |
| 145 | strdata[1] = cpu_to_fdt32(string_size); |
| 146 | if (!ret) { |
| 147 | ret = fdt_setprop(fit, noffset, "hashed-strings", |
| 148 | strdata, sizeof(strdata)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | static int fit_image_setup_sig(struct image_sign_info *info, |
| 156 | const char *keydir, void *fit, const char *image_name, |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 157 | int noffset, const char *require_keys, const char *engine_id) |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 158 | { |
| 159 | const char *node_name; |
| 160 | char *algo_name; |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 161 | const char *padding_name; |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 162 | |
| 163 | node_name = fit_get_name(fit, noffset, NULL); |
| 164 | if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { |
| 165 | printf("Can't get algo property for '%s' signature node in '%s' image node\n", |
| 166 | node_name, image_name); |
| 167 | return -1; |
| 168 | } |
| 169 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 170 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 171 | |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 172 | memset(info, '\0', sizeof(*info)); |
| 173 | info->keydir = keydir; |
Simon Glass | 72188f5 | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 174 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 175 | info->fit = fit; |
| 176 | info->node_offset = noffset; |
Masahiro Yamada | 1d88a99 | 2017-10-27 13:25:21 +0900 | [diff] [blame] | 177 | info->name = strdup(algo_name); |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 178 | info->checksum = image_get_checksum_algo(algo_name); |
| 179 | info->crypto = image_get_crypto_algo(algo_name); |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 180 | info->padding = image_get_padding_algo(padding_name); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 181 | info->require_keys = require_keys; |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 182 | info->engine_id = engine_id; |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 183 | if (!info->checksum || !info->crypto) { |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 184 | printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n", |
| 185 | algo_name, node_name, image_name); |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * fit_image_process_sig- Process a single subnode of the images/ node |
| 194 | * |
| 195 | * Check each subnode and process accordingly. For signature nodes we |
| 196 | * generate a signed hash of the supplised data and store it in the node. |
| 197 | * |
| 198 | * @keydir: Directory containing keys to use for signing |
| 199 | * @keydest: Destination FDT blob to write public keys into |
| 200 | * @fit: pointer to the FIT format image header |
| 201 | * @image_name: name of image being processes (used to display errors) |
| 202 | * @noffset: subnode offset |
| 203 | * @data: data to process |
| 204 | * @size: size of data in bytes |
| 205 | * @comment: Comment to add to signature nodes |
| 206 | * @require_keys: Mark all keys as 'required' |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 207 | * @engine_id: Engine to use for signing |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 208 | * @return 0 if ok, -1 on error |
| 209 | */ |
| 210 | static int fit_image_process_sig(const char *keydir, void *keydest, |
| 211 | void *fit, const char *image_name, |
| 212 | int noffset, const void *data, size_t size, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 213 | const char *comment, int require_keys, const char *engine_id, |
| 214 | const char *cmdname) |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 215 | { |
| 216 | struct image_sign_info info; |
| 217 | struct image_region region; |
| 218 | const char *node_name; |
| 219 | uint8_t *value; |
| 220 | uint value_len; |
| 221 | int ret; |
| 222 | |
| 223 | if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset, |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 224 | require_keys ? "image" : NULL, engine_id)) |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 225 | return -1; |
| 226 | |
| 227 | node_name = fit_get_name(fit, noffset, NULL); |
| 228 | region.data = data; |
| 229 | region.size = size; |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 230 | ret = info.crypto->sign(&info, ®ion, 1, &value, &value_len); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 231 | if (ret) { |
| 232 | printf("Failed to sign '%s' signature node in '%s' image node: %d\n", |
| 233 | node_name, image_name, ret); |
| 234 | |
| 235 | /* We allow keys to be missing */ |
| 236 | if (ret == -ENOENT) |
| 237 | return 0; |
| 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 242 | NULL, 0, cmdname); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 243 | if (ret) { |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 244 | if (ret == -FDT_ERR_NOSPACE) |
| 245 | return -ENOSPC; |
| 246 | printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 247 | node_name, image_name, fdt_strerror(ret)); |
| 248 | return -1; |
| 249 | } |
| 250 | free(value); |
| 251 | |
| 252 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
Simon Glass | 72188f5 | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 253 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 254 | |
mario.six@gdsys.cc | 713fb2d | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 255 | /* |
| 256 | * Write the public key into the supplied FDT file; this might fail |
mario.six@gdsys.cc | c236ebd | 2016-07-19 11:07:06 +0200 | [diff] [blame] | 257 | * several times, since we try signing with successively increasing |
mario.six@gdsys.cc | 713fb2d | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 258 | * size values |
| 259 | */ |
Masahiro Yamada | 6793d01 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 260 | if (keydest) { |
| 261 | ret = info.crypto->add_verify_data(&info, keydest); |
| 262 | if (ret) { |
| 263 | printf("Failed to add verification data for '%s' signature node in '%s' image node\n", |
| 264 | node_name, image_name); |
| 265 | return ret; |
| 266 | } |
| 267 | } |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 272 | static int fit_image_read_data(char *filename, unsigned char *data, |
| 273 | int expected_size) |
| 274 | { |
| 275 | struct stat sbuf; |
| 276 | int fd, ret = -1; |
| 277 | ssize_t n; |
| 278 | |
| 279 | /* Open file */ |
| 280 | fd = open(filename, O_RDONLY | O_BINARY); |
| 281 | if (fd < 0) { |
| 282 | printf("Can't open file %s (err=%d => %s)\n", |
| 283 | filename, errno, strerror(errno)); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | /* Compute file size */ |
| 288 | if (fstat(fd, &sbuf) < 0) { |
| 289 | printf("Can't fstat file %s (err=%d => %s)\n", |
| 290 | filename, errno, strerror(errno)); |
| 291 | goto err; |
| 292 | } |
| 293 | |
| 294 | /* Check file size */ |
| 295 | if (sbuf.st_size != expected_size) { |
Heinrich Schuchardt | 3311eda | 2020-10-08 20:51:24 +0200 | [diff] [blame] | 296 | printf("File %s don't have the expected size (size=%lld, expected=%d)\n", |
| 297 | filename, (long long)sbuf.st_size, expected_size); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 298 | goto err; |
| 299 | } |
| 300 | |
| 301 | /* Read data */ |
| 302 | n = read(fd, data, sbuf.st_size); |
| 303 | if (n < 0) { |
| 304 | printf("Can't read file %s (err=%d => %s)\n", |
| 305 | filename, errno, strerror(errno)); |
| 306 | goto err; |
| 307 | } |
| 308 | |
| 309 | /* Check that we have read all the file */ |
| 310 | if (n != sbuf.st_size) { |
Heinrich Schuchardt | 3311eda | 2020-10-08 20:51:24 +0200 | [diff] [blame] | 311 | printf("Can't read all file %s (read %zd bytes, expexted %lld)\n", |
| 312 | filename, n, (long long)sbuf.st_size); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 313 | goto err; |
| 314 | } |
| 315 | |
| 316 | ret = 0; |
| 317 | |
| 318 | err: |
| 319 | close(fd); |
| 320 | return ret; |
| 321 | } |
| 322 | |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 323 | static int get_random_data(void *data, int size) |
| 324 | { |
| 325 | unsigned char *tmp = data; |
| 326 | struct timespec date; |
| 327 | int i, ret = 0; |
| 328 | |
| 329 | if (!tmp) { |
| 330 | printf("%s: pointer data is NULL\n", __func__); |
| 331 | ret = -1; |
| 332 | goto out; |
| 333 | } |
| 334 | |
| 335 | ret = clock_gettime(CLOCK_MONOTONIC, &date); |
| 336 | if (ret < 0) { |
| 337 | printf("%s: clock_gettime has failed (err=%d, str=%s)\n", |
Philippe Reynes | 2692749 | 2020-11-13 15:15:18 +0100 | [diff] [blame] | 338 | __func__, ret, strerror(errno)); |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 339 | goto out; |
| 340 | } |
| 341 | |
Philippe Reynes | cc34f04 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 342 | srandom(date.tv_nsec); |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 343 | |
| 344 | for (i = 0; i < size; i++) { |
Philippe Reynes | cc34f04 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 345 | *tmp = random() & 0xff; |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 346 | tmp++; |
| 347 | } |
| 348 | |
| 349 | out: |
| 350 | return ret; |
| 351 | } |
| 352 | |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 353 | static int fit_image_setup_cipher(struct image_cipher_info *info, |
| 354 | const char *keydir, void *fit, |
| 355 | const char *image_name, int image_noffset, |
Patrick Oppenlander | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 356 | int noffset) |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 357 | { |
| 358 | char *algo_name; |
| 359 | char filename[128]; |
| 360 | int ret = -1; |
| 361 | |
| 362 | if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) { |
Patrick Oppenlander | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 363 | printf("Can't get algo name for cipher in image '%s'\n", |
| 364 | image_name); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 365 | goto out; |
| 366 | } |
| 367 | |
| 368 | info->keydir = keydir; |
| 369 | |
| 370 | /* Read the key name */ |
Simon Glass | 72188f5 | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 371 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 372 | if (!info->keyname) { |
Patrick Oppenlander | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 373 | printf("Can't get key name for cipher in image '%s'\n", |
| 374 | image_name); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 375 | goto out; |
| 376 | } |
| 377 | |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 378 | /* |
| 379 | * Read the IV name |
| 380 | * |
| 381 | * If this property is not provided then mkimage will generate |
| 382 | * a random IV and store it in the FIT image |
| 383 | */ |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 384 | info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 385 | |
| 386 | info->fit = fit; |
| 387 | info->node_noffset = noffset; |
| 388 | info->name = algo_name; |
| 389 | |
| 390 | info->cipher = image_get_cipher_algo(algo_name); |
| 391 | if (!info->cipher) { |
| 392 | printf("Can't get algo for cipher '%s'\n", image_name); |
| 393 | goto out; |
| 394 | } |
| 395 | |
| 396 | /* Read the key in the file */ |
| 397 | snprintf(filename, sizeof(filename), "%s/%s%s", |
| 398 | info->keydir, info->keyname, ".bin"); |
| 399 | info->key = malloc(info->cipher->key_len); |
| 400 | if (!info->key) { |
| 401 | printf("Can't allocate memory for key\n"); |
| 402 | ret = -1; |
| 403 | goto out; |
| 404 | } |
| 405 | ret = fit_image_read_data(filename, (unsigned char *)info->key, |
| 406 | info->cipher->key_len); |
| 407 | if (ret < 0) |
| 408 | goto out; |
| 409 | |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 410 | info->iv = malloc(info->cipher->iv_len); |
| 411 | if (!info->iv) { |
| 412 | printf("Can't allocate memory for iv\n"); |
| 413 | ret = -1; |
| 414 | goto out; |
| 415 | } |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 416 | |
| 417 | if (info->ivname) { |
| 418 | /* Read the IV in the file */ |
| 419 | snprintf(filename, sizeof(filename), "%s/%s%s", |
| 420 | info->keydir, info->ivname, ".bin"); |
| 421 | ret = fit_image_read_data(filename, (unsigned char *)info->iv, |
| 422 | info->cipher->iv_len); |
| 423 | } else { |
| 424 | /* Generate an ramdom IV */ |
| 425 | ret = get_random_data((void *)info->iv, info->cipher->iv_len); |
| 426 | } |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 427 | |
| 428 | out: |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | int fit_image_write_cipher(void *fit, int image_noffset, int noffset, |
| 433 | const void *data, size_t size, |
| 434 | unsigned char *data_ciphered, int data_ciphered_len) |
| 435 | { |
| 436 | int ret = -1; |
| 437 | |
Patrick Oppenlander | 04aeebb | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 438 | /* Replace data with ciphered data */ |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 439 | ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP, |
| 440 | data_ciphered, data_ciphered_len); |
Patrick Oppenlander | 04aeebb | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 441 | if (ret == -FDT_ERR_NOSPACE) { |
| 442 | ret = -ENOSPC; |
| 443 | goto out; |
| 444 | } |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 445 | if (ret) { |
Patrick Oppenlander | 04aeebb | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 446 | printf("Can't replace data with ciphered data (err = %d)\n", ret); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 447 | goto out; |
| 448 | } |
| 449 | |
| 450 | /* add non ciphered data size */ |
| 451 | ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size); |
Patrick Oppenlander | 04aeebb | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 452 | if (ret == -FDT_ERR_NOSPACE) { |
| 453 | ret = -ENOSPC; |
| 454 | goto out; |
| 455 | } |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 456 | if (ret) { |
| 457 | printf("Can't add unciphered data size (err = %d)\n", ret); |
| 458 | goto out; |
| 459 | } |
| 460 | |
| 461 | out: |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | static int |
| 466 | fit_image_process_cipher(const char *keydir, void *keydest, void *fit, |
| 467 | const char *image_name, int image_noffset, |
Patrick Oppenlander | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 468 | int node_noffset, const void *data, size_t size, |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 469 | const char *cmdname) |
| 470 | { |
| 471 | struct image_cipher_info info; |
| 472 | unsigned char *data_ciphered = NULL; |
| 473 | int data_ciphered_len; |
| 474 | int ret; |
| 475 | |
| 476 | memset(&info, 0, sizeof(info)); |
| 477 | |
| 478 | ret = fit_image_setup_cipher(&info, keydir, fit, image_name, |
Patrick Oppenlander | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 479 | image_noffset, node_noffset); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 480 | if (ret) |
| 481 | goto out; |
| 482 | |
| 483 | ret = info.cipher->encrypt(&info, data, size, |
| 484 | &data_ciphered, &data_ciphered_len); |
| 485 | if (ret) |
| 486 | goto out; |
| 487 | |
| 488 | /* |
| 489 | * Write the public key into the supplied FDT file; this might fail |
| 490 | * several times, since we try signing with successively increasing |
| 491 | * size values |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 492 | * And, if needed, write the iv in the FIT file |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 493 | */ |
| 494 | if (keydest) { |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 495 | ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 496 | if (ret) { |
| 497 | printf("Failed to add verification data for cipher '%s' in image '%s'\n", |
| 498 | info.keyname, image_name); |
| 499 | goto out; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | ret = fit_image_write_cipher(fit, image_noffset, node_noffset, |
| 504 | data, size, |
| 505 | data_ciphered, data_ciphered_len); |
| 506 | |
| 507 | out: |
| 508 | free(data_ciphered); |
| 509 | free((void *)info.key); |
| 510 | free((void *)info.iv); |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | int fit_image_cipher_data(const char *keydir, void *keydest, |
| 515 | void *fit, int image_noffset, const char *comment, |
| 516 | int require_keys, const char *engine_id, |
| 517 | const char *cmdname) |
| 518 | { |
| 519 | const char *image_name; |
| 520 | const void *data; |
| 521 | size_t size; |
Patrick Oppenlander | b33e5cc | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 522 | int cipher_node_offset, len; |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 523 | |
| 524 | /* Get image name */ |
| 525 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 526 | if (!image_name) { |
| 527 | printf("Can't get image name\n"); |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | /* Get image data and data length */ |
| 532 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
| 533 | printf("Can't get image data/size\n"); |
| 534 | return -1; |
| 535 | } |
| 536 | |
Patrick Oppenlander | b33e5cc | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 537 | /* |
| 538 | * Don't cipher ciphered data. |
| 539 | * |
| 540 | * If the data-size-unciphered property is present the data for this |
| 541 | * image is already encrypted. This is important as 'mkimage -F' can be |
| 542 | * run multiple times on a FIT image. |
| 543 | */ |
| 544 | if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len)) |
| 545 | return 0; |
| 546 | if (len != -FDT_ERR_NOTFOUND) { |
| 547 | printf("Failure testing for data-size-unciphered\n"); |
| 548 | return -1; |
| 549 | } |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 550 | |
Patrick Oppenlander | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 551 | /* Process cipher node if present */ |
| 552 | cipher_node_offset = fdt_subnode_offset(fit, image_noffset, |
| 553 | FIT_CIPHER_NODENAME); |
| 554 | if (cipher_node_offset == -FDT_ERR_NOTFOUND) |
| 555 | return 0; |
| 556 | if (cipher_node_offset < 0) { |
| 557 | printf("Failure getting cipher node\n"); |
| 558 | return -1; |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 559 | } |
Patrick Oppenlander | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 560 | if (!IMAGE_ENABLE_ENCRYPT || !keydir) |
| 561 | return 0; |
| 562 | return fit_image_process_cipher(keydir, keydest, fit, image_name, |
| 563 | image_noffset, cipher_node_offset, data, size, cmdname); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 564 | } |
| 565 | |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 566 | /** |
| 567 | * fit_image_add_verification_data() - calculate/set verig. data for image node |
| 568 | * |
| 569 | * This adds hash and signature values for an component image node. |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 570 | * |
| 571 | * All existing hash subnodes are checked, if algorithm property is set to |
| 572 | * one of the supported hash algorithms, hash value is computed and |
| 573 | * corresponding hash node property is set, for example: |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 574 | * |
| 575 | * Input component image node structure: |
| 576 | * |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 577 | * o image-1 (at image_noffset) |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 578 | * | - data = [binary data] |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 579 | * o hash-1 |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 580 | * |- algo = "sha1" |
| 581 | * |
| 582 | * Output component image node structure: |
| 583 | * |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 584 | * o image-1 (at image_noffset) |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 585 | * | - data = [binary data] |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 586 | * o hash-1 |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 587 | * |- algo = "sha1" |
| 588 | * |- value = sha1(data) |
| 589 | * |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 590 | * For signature details, please see doc/uImage.FIT/signature.txt |
| 591 | * |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 592 | * @keydir Directory containing *.key and *.crt files (or NULL) |
| 593 | * @keydest FDT Blob to write public keys into (NULL if none) |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 594 | * @fit: Pointer to the FIT format image header |
| 595 | * @image_noffset: Requested component image node |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 596 | * @comment: Comment to add to signature nodes |
| 597 | * @require_keys: Mark all keys as 'required' |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 598 | * @engine_id: Engine to use for signing |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 599 | * @return: 0 on success, <0 on failure |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 600 | */ |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 601 | int fit_image_add_verification_data(const char *keydir, void *keydest, |
| 602 | void *fit, int image_noffset, const char *comment, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 603 | int require_keys, const char *engine_id, const char *cmdname) |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 604 | { |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 605 | const char *image_name; |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 606 | const void *data; |
| 607 | size_t size; |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 608 | int noffset; |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 609 | |
| 610 | /* Get image data and data length */ |
| 611 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
| 612 | printf("Can't get image data/size\n"); |
| 613 | return -1; |
| 614 | } |
| 615 | |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 616 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 617 | |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 618 | /* Process all hash subnodes of the component image node */ |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 619 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 620 | noffset >= 0; |
| 621 | noffset = fdt_next_subnode(fit, noffset)) { |
| 622 | const char *node_name; |
| 623 | int ret = 0; |
| 624 | |
| 625 | /* |
| 626 | * Check subnode name, must be equal to "hash" or "signature". |
| 627 | * Multiple hash nodes require unique unit node |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 628 | * names, e.g. hash-1, hash-2, signature-1, etc. |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 629 | */ |
| 630 | node_name = fit_get_name(fit, noffset, NULL); |
| 631 | if (!strncmp(node_name, FIT_HASH_NODENAME, |
| 632 | strlen(FIT_HASH_NODENAME))) { |
| 633 | ret = fit_image_process_hash(fit, image_name, noffset, |
| 634 | data, size); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 635 | } else if (IMAGE_ENABLE_SIGN && keydir && |
| 636 | !strncmp(node_name, FIT_SIG_NODENAME, |
| 637 | strlen(FIT_SIG_NODENAME))) { |
| 638 | ret = fit_image_process_sig(keydir, keydest, |
| 639 | fit, image_name, noffset, data, size, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 640 | comment, require_keys, engine_id, cmdname); |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 641 | } |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 642 | if (ret) |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 643 | return ret; |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 649 | struct strlist { |
| 650 | int count; |
| 651 | char **strings; |
| 652 | }; |
| 653 | |
| 654 | static void strlist_init(struct strlist *list) |
| 655 | { |
| 656 | memset(list, '\0', sizeof(*list)); |
| 657 | } |
| 658 | |
| 659 | static void strlist_free(struct strlist *list) |
| 660 | { |
| 661 | int i; |
| 662 | |
| 663 | for (i = 0; i < list->count; i++) |
| 664 | free(list->strings[i]); |
| 665 | free(list->strings); |
| 666 | } |
| 667 | |
| 668 | static int strlist_add(struct strlist *list, const char *str) |
| 669 | { |
| 670 | char *dup; |
| 671 | |
| 672 | dup = strdup(str); |
| 673 | list->strings = realloc(list->strings, |
| 674 | (list->count + 1) * sizeof(char *)); |
| 675 | if (!list || !str) |
| 676 | return -1; |
| 677 | list->strings[list->count++] = dup; |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static const char *fit_config_get_image_list(void *fit, int noffset, |
| 683 | int *lenp, int *allow_missingp) |
| 684 | { |
| 685 | static const char default_list[] = FIT_KERNEL_PROP "\0" |
| 686 | FIT_FDT_PROP; |
| 687 | const char *prop; |
| 688 | |
| 689 | /* If there is an "image" property, use that */ |
| 690 | prop = fdt_getprop(fit, noffset, "sign-images", lenp); |
| 691 | if (prop) { |
| 692 | *allow_missingp = 0; |
| 693 | return *lenp ? prop : NULL; |
| 694 | } |
| 695 | |
| 696 | /* Default image list */ |
| 697 | *allow_missingp = 1; |
| 698 | *lenp = sizeof(default_list); |
| 699 | |
| 700 | return default_list; |
| 701 | } |
| 702 | |
Philippe Reynes | 5a4116f | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 703 | static int fit_config_add_hash(void *fit, const char *conf_name, const char *sig_name, |
| 704 | struct strlist *node_inc, const char *iname, int image_noffset) |
| 705 | { |
| 706 | char name[200], path[200]; |
| 707 | int noffset; |
| 708 | int hash_count; |
| 709 | int ret; |
| 710 | |
| 711 | ret = fdt_get_path(fit, image_noffset, path, sizeof(path)); |
| 712 | if (ret < 0) |
| 713 | goto err_path; |
| 714 | if (strlist_add(node_inc, path)) |
| 715 | goto err_mem; |
| 716 | |
| 717 | snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, |
| 718 | conf_name); |
| 719 | |
| 720 | /* Add all this image's hashes */ |
| 721 | hash_count = 0; |
| 722 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 723 | noffset >= 0; |
| 724 | noffset = fdt_next_subnode(fit, noffset)) { |
| 725 | const char *name = fit_get_name(fit, noffset, NULL); |
| 726 | |
| 727 | if (strncmp(name, FIT_HASH_NODENAME, |
| 728 | strlen(FIT_HASH_NODENAME))) |
| 729 | continue; |
| 730 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 731 | if (ret < 0) |
| 732 | goto err_path; |
| 733 | if (strlist_add(node_inc, path)) |
| 734 | goto err_mem; |
| 735 | hash_count++; |
| 736 | } |
| 737 | |
| 738 | if (!hash_count) { |
| 739 | printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n", |
| 740 | conf_name, sig_name, iname); |
| 741 | return -ENOMSG; |
| 742 | } |
| 743 | |
| 744 | /* Add this image's cipher node if present */ |
| 745 | noffset = fdt_subnode_offset(fit, image_noffset, |
| 746 | FIT_CIPHER_NODENAME); |
| 747 | if (noffset != -FDT_ERR_NOTFOUND) { |
| 748 | if (noffset < 0) { |
| 749 | printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n", |
| 750 | conf_name, sig_name, iname, |
| 751 | fdt_strerror(noffset)); |
| 752 | return -EIO; |
| 753 | } |
| 754 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 755 | if (ret < 0) |
| 756 | goto err_path; |
| 757 | if (strlist_add(node_inc, path)) |
| 758 | goto err_mem; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | |
| 763 | err_mem: |
| 764 | printf("Out of memory processing configuration '%s/%s'\n", conf_name, |
| 765 | sig_name); |
| 766 | return -ENOMEM; |
| 767 | |
| 768 | err_path: |
| 769 | printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n", |
| 770 | iname, conf_name, sig_name, fdt_strerror(ret)); |
| 771 | return -ENOENT; |
| 772 | } |
| 773 | |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 774 | static int fit_config_get_hash_list(void *fit, int conf_noffset, |
| 775 | int sig_offset, struct strlist *node_inc) |
| 776 | { |
| 777 | int allow_missing; |
| 778 | const char *prop, *iname, *end; |
| 779 | const char *conf_name, *sig_name; |
Philippe Reynes | 5a4116f | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 780 | char name[200]; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 781 | int image_count; |
| 782 | int ret, len; |
| 783 | |
| 784 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 785 | sig_name = fit_get_name(fit, sig_offset, NULL); |
| 786 | |
| 787 | /* |
| 788 | * Build a list of nodes we need to hash. We always need the root |
| 789 | * node and the configuration. |
| 790 | */ |
| 791 | strlist_init(node_inc); |
| 792 | snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name); |
| 793 | if (strlist_add(node_inc, "/") || |
| 794 | strlist_add(node_inc, name)) |
| 795 | goto err_mem; |
| 796 | |
| 797 | /* Get a list of images that we intend to sign */ |
Heiko Schocher | 66b36f8 | 2014-03-03 12:19:23 +0100 | [diff] [blame] | 798 | prop = fit_config_get_image_list(fit, sig_offset, &len, |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 799 | &allow_missing); |
| 800 | if (!prop) |
| 801 | return 0; |
| 802 | |
| 803 | /* Locate the images */ |
| 804 | end = prop + len; |
| 805 | image_count = 0; |
| 806 | for (iname = prop; iname < end; iname += strlen(iname) + 1) { |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 807 | int image_noffset; |
Philippe Reynes | edfeba7 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 808 | int index, max_index; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 809 | |
Philippe Reynes | edfeba7 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 810 | max_index = fdt_stringlist_count(fit, conf_noffset, iname); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 811 | |
Philippe Reynes | edfeba7 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 812 | for (index = 0; index < max_index; index++) { |
| 813 | image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset, |
| 814 | iname, index); |
| 815 | |
| 816 | if (image_noffset < 0) { |
| 817 | printf("Failed to find image '%s' in configuration '%s/%s'\n", |
| 818 | iname, conf_name, sig_name); |
| 819 | if (allow_missing) |
| 820 | continue; |
| 821 | |
| 822 | return -ENOENT; |
| 823 | } |
| 824 | |
| 825 | ret = fit_config_add_hash(fit, conf_name, |
| 826 | sig_name, node_inc, |
| 827 | iname, image_noffset); |
| 828 | if (ret < 0) |
| 829 | return ret; |
| 830 | |
| 831 | image_count++; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 832 | } |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if (!image_count) { |
| 836 | printf("Failed to find any images for configuration '%s/%s'\n", |
| 837 | conf_name, sig_name); |
| 838 | return -ENOMSG; |
| 839 | } |
| 840 | |
| 841 | return 0; |
| 842 | |
| 843 | err_mem: |
| 844 | printf("Out of memory processing configuration '%s/%s'\n", conf_name, |
| 845 | sig_name); |
| 846 | return -ENOMEM; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | static int fit_config_get_data(void *fit, int conf_noffset, int noffset, |
| 850 | struct image_region **regionp, int *region_countp, |
| 851 | char **region_propp, int *region_proplen) |
| 852 | { |
| 853 | char * const exc_prop[] = {"data"}; |
| 854 | struct strlist node_inc; |
| 855 | struct image_region *region; |
| 856 | struct fdt_region fdt_regions[100]; |
| 857 | const char *conf_name, *sig_name; |
| 858 | char path[200]; |
| 859 | int count, i; |
| 860 | char *region_prop; |
| 861 | int ret, len; |
| 862 | |
| 863 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
Masahiro Yamada | 16067e6 | 2017-10-19 19:16:21 +0900 | [diff] [blame] | 864 | sig_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 865 | debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name); |
| 866 | |
| 867 | /* Get a list of nodes we want to hash */ |
| 868 | ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc); |
| 869 | if (ret) |
| 870 | return ret; |
| 871 | |
| 872 | /* Get a list of regions to hash */ |
| 873 | count = fdt_find_regions(fit, node_inc.strings, node_inc.count, |
| 874 | exc_prop, ARRAY_SIZE(exc_prop), |
| 875 | fdt_regions, ARRAY_SIZE(fdt_regions), |
| 876 | path, sizeof(path), 1); |
| 877 | if (count < 0) { |
| 878 | printf("Failed to hash configuration '%s/%s': %s\n", conf_name, |
| 879 | sig_name, fdt_strerror(ret)); |
| 880 | return -EIO; |
| 881 | } |
| 882 | if (count == 0) { |
| 883 | printf("No data to hash for configuration '%s/%s': %s\n", |
| 884 | conf_name, sig_name, fdt_strerror(ret)); |
| 885 | return -EINVAL; |
| 886 | } |
| 887 | |
| 888 | /* Build our list of data blocks */ |
| 889 | region = fit_region_make_list(fit, fdt_regions, count, NULL); |
| 890 | if (!region) { |
| 891 | printf("Out of memory hashing configuration '%s/%s'\n", |
| 892 | conf_name, sig_name); |
| 893 | return -ENOMEM; |
| 894 | } |
| 895 | |
| 896 | /* Create a list of all hashed properties */ |
| 897 | debug("Hash nodes:\n"); |
| 898 | for (i = len = 0; i < node_inc.count; i++) { |
| 899 | debug(" %s\n", node_inc.strings[i]); |
| 900 | len += strlen(node_inc.strings[i]) + 1; |
| 901 | } |
| 902 | region_prop = malloc(len); |
| 903 | if (!region_prop) { |
| 904 | printf("Out of memory setting up regions for configuration '%s/%s'\n", |
| 905 | conf_name, sig_name); |
| 906 | return -ENOMEM; |
| 907 | } |
| 908 | for (i = len = 0; i < node_inc.count; |
| 909 | len += strlen(node_inc.strings[i]) + 1, i++) |
| 910 | strcpy(region_prop + len, node_inc.strings[i]); |
| 911 | strlist_free(&node_inc); |
| 912 | |
| 913 | *region_countp = count; |
| 914 | *regionp = region; |
| 915 | *region_propp = region_prop; |
| 916 | *region_proplen = len; |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static int fit_config_process_sig(const char *keydir, void *keydest, |
| 922 | void *fit, const char *conf_name, int conf_noffset, |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 923 | int noffset, const char *comment, int require_keys, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 924 | const char *engine_id, const char *cmdname) |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 925 | { |
| 926 | struct image_sign_info info; |
| 927 | const char *node_name; |
| 928 | struct image_region *region; |
| 929 | char *region_prop; |
| 930 | int region_proplen; |
| 931 | int region_count; |
| 932 | uint8_t *value; |
| 933 | uint value_len; |
| 934 | int ret; |
| 935 | |
| 936 | node_name = fit_get_name(fit, noffset, NULL); |
| 937 | if (fit_config_get_data(fit, conf_noffset, noffset, ®ion, |
| 938 | ®ion_count, ®ion_prop, ®ion_proplen)) |
| 939 | return -1; |
| 940 | |
| 941 | if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset, |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 942 | require_keys ? "conf" : NULL, engine_id)) |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 943 | return -1; |
| 944 | |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 945 | ret = info.crypto->sign(&info, region, region_count, &value, |
| 946 | &value_len); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 947 | free(region); |
| 948 | if (ret) { |
| 949 | printf("Failed to sign '%s' signature node in '%s' conf node\n", |
| 950 | node_name, conf_name); |
| 951 | |
| 952 | /* We allow keys to be missing */ |
| 953 | if (ret == -ENOENT) |
| 954 | return 0; |
| 955 | return -1; |
| 956 | } |
| 957 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 958 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 959 | region_prop, region_proplen, cmdname); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 960 | if (ret) { |
| 961 | if (ret == -FDT_ERR_NOSPACE) |
| 962 | return -ENOSPC; |
| 963 | printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
| 964 | node_name, conf_name, fdt_strerror(ret)); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 965 | return -1; |
| 966 | } |
| 967 | free(value); |
| 968 | free(region_prop); |
| 969 | |
| 970 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
Simon Glass | 72188f5 | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 971 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 972 | |
| 973 | /* Write the public key into the supplied FDT file */ |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 974 | if (keydest) { |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 975 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 976 | if (ret) { |
Masahiro Yamada | 76b9cba | 2017-10-27 15:04:21 +0900 | [diff] [blame] | 977 | printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n", |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 978 | node_name, conf_name); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 979 | } |
Simon Glass | 597a8b2 | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 980 | return ret; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | static int fit_config_add_verification_data(const char *keydir, void *keydest, |
| 987 | void *fit, int conf_noffset, const char *comment, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 988 | int require_keys, const char *engine_id, const char *cmdname) |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 989 | { |
| 990 | const char *conf_name; |
| 991 | int noffset; |
| 992 | |
| 993 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 994 | |
| 995 | /* Process all hash subnodes of the configuration node */ |
| 996 | for (noffset = fdt_first_subnode(fit, conf_noffset); |
| 997 | noffset >= 0; |
| 998 | noffset = fdt_next_subnode(fit, noffset)) { |
| 999 | const char *node_name; |
| 1000 | int ret = 0; |
| 1001 | |
| 1002 | node_name = fit_get_name(fit, noffset, NULL); |
| 1003 | if (!strncmp(node_name, FIT_SIG_NODENAME, |
| 1004 | strlen(FIT_SIG_NODENAME))) { |
| 1005 | ret = fit_config_process_sig(keydir, keydest, |
| 1006 | fit, conf_name, conf_noffset, noffset, comment, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 1007 | require_keys, engine_id, cmdname); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1008 | } |
| 1009 | if (ret) |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 1016 | int fit_cipher_data(const char *keydir, void *keydest, void *fit, |
| 1017 | const char *comment, int require_keys, |
| 1018 | const char *engine_id, const char *cmdname) |
| 1019 | { |
| 1020 | int images_noffset; |
| 1021 | int noffset; |
| 1022 | int ret; |
| 1023 | |
| 1024 | /* Find images parent node offset */ |
| 1025 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1026 | if (images_noffset < 0) { |
| 1027 | printf("Can't find images parent node '%s' (%s)\n", |
| 1028 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1029 | return images_noffset; |
| 1030 | } |
| 1031 | |
| 1032 | /* Process its subnodes, print out component images details */ |
| 1033 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1034 | noffset >= 0; |
| 1035 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1036 | /* |
| 1037 | * Direct child node of the images parent node, |
| 1038 | * i.e. component image node. |
| 1039 | */ |
| 1040 | ret = fit_image_cipher_data(keydir, keydest, |
| 1041 | fit, noffset, comment, |
| 1042 | require_keys, engine_id, |
| 1043 | cmdname); |
| 1044 | if (ret) |
| 1045 | return ret; |
| 1046 | } |
| 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1051 | int fit_add_verification_data(const char *keydir, void *keydest, void *fit, |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 1052 | const char *comment, int require_keys, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 1053 | const char *engine_id, const char *cmdname) |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1054 | { |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1055 | int images_noffset, confs_noffset; |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1056 | int noffset; |
| 1057 | int ret; |
| 1058 | |
| 1059 | /* Find images parent node offset */ |
| 1060 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1061 | if (images_noffset < 0) { |
| 1062 | printf("Can't find images parent node '%s' (%s)\n", |
| 1063 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1064 | return images_noffset; |
| 1065 | } |
| 1066 | |
| 1067 | /* Process its subnodes, print out component images details */ |
| 1068 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1069 | noffset >= 0; |
| 1070 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1071 | /* |
| 1072 | * Direct child node of the images parent node, |
| 1073 | * i.e. component image node. |
| 1074 | */ |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1075 | ret = fit_image_add_verification_data(keydir, keydest, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 1076 | fit, noffset, comment, require_keys, engine_id, |
| 1077 | cmdname); |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1078 | if (ret) |
| 1079 | return ret; |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1082 | /* If there are no keys, we can't sign configurations */ |
| 1083 | if (!IMAGE_ENABLE_SIGN || !keydir) |
| 1084 | return 0; |
| 1085 | |
| 1086 | /* Find configurations parent node offset */ |
| 1087 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1088 | if (confs_noffset < 0) { |
| 1089 | printf("Can't find images parent node '%s' (%s)\n", |
Heiko Schocher | 04a710a | 2014-08-11 11:02:17 +0200 | [diff] [blame] | 1090 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1091 | return -ENOENT; |
| 1092 | } |
| 1093 | |
| 1094 | /* Process its subnodes, print out component images details */ |
| 1095 | for (noffset = fdt_first_subnode(fit, confs_noffset); |
| 1096 | noffset >= 0; |
| 1097 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1098 | ret = fit_config_add_verification_data(keydir, keydest, |
| 1099 | fit, noffset, comment, |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 1100 | require_keys, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 1101 | engine_id, cmdname); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1102 | if (ret) |
| 1103 | return ret; |
| 1104 | } |
| 1105 | |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1108 | |
| 1109 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1110 | int fit_check_sign(const void *fit, const void *key, |
| 1111 | const char *fit_uname_config) |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1112 | { |
| 1113 | int cfg_noffset; |
| 1114 | int ret; |
| 1115 | |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1116 | cfg_noffset = fit_conf_get_node(fit, fit_uname_config); |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1117 | if (!cfg_noffset) |
| 1118 | return -1; |
| 1119 | |
Simon Glass | 382cf62 | 2020-03-18 11:43:56 -0600 | [diff] [blame] | 1120 | printf("Verifying Hash Integrity for node '%s'... ", |
| 1121 | fdt_get_name(fit, cfg_noffset, NULL)); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1122 | ret = fit_config_verify(fit, cfg_noffset); |
| 1123 | if (ret) |
| 1124 | return ret; |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1125 | printf("Verified OK, loading images\n"); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1126 | ret = bootm_host_load_images(fit, cfg_noffset); |
| 1127 | |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | #endif |