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