Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
| 4 | * |
| 5 | * (C) Copyright 2008 Semihalf |
| 6 | * |
| 7 | * (C) Copyright 2000-2006 |
| 8 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "mkimage.h" |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 12 | #include <bootm.h> |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 13 | #include <image.h> |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 14 | #include <version.h> |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 15 | |
| 16 | /** |
Simon Glass | 0ce6e3d | 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 | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 40 | return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
Simon Glass | 0ce6e3d | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /** |
Simon Glass | 25d6e6b | 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 | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 63 | const char *node_name; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 64 | int value_len; |
| 65 | char *algo; |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 66 | int ret; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 67 | |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 68 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 25d6e6b | 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 | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 72 | node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 73 | return -ENOENT; |
Simon Glass | 25d6e6b | 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 | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 78 | algo, node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 79 | return -EPROTONOSUPPORT; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Simon Glass | ec539f7 | 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 | 25d6e6b | 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 | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 85 | node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 86 | return ret; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /** |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 93 | * fit_image_write_sig() - write the signature to a FIT |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 94 | * |
Simon Glass | fbabc0f | 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 | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 109 | int region_proplen, const char *cmdname) |
Simon Glass | fbabc0f | 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 | 697fcdc | 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 | fbabc0f | 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 | 9767bf4 | 2018-06-09 11:45:20 -0400 | [diff] [blame] | 142 | /* This is a legacy offset, it is unused, and must remain 0. */ |
Simon Glass | fbabc0f | 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 | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 156 | int noffset, const char *require_keys, const char *engine_id) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 157 | { |
| 158 | const char *node_name; |
| 159 | char *algo_name; |
| 160 | |
| 161 | node_name = fit_get_name(fit, noffset, NULL); |
| 162 | if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { |
| 163 | printf("Can't get algo property for '%s' signature node in '%s' image node\n", |
| 164 | node_name, image_name); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | memset(info, '\0', sizeof(*info)); |
| 169 | info->keydir = keydir; |
| 170 | info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); |
| 171 | info->fit = fit; |
| 172 | info->node_offset = noffset; |
Masahiro Yamada | c1f137b | 2017-10-27 13:25:21 +0900 | [diff] [blame] | 173 | info->name = strdup(algo_name); |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 174 | info->checksum = image_get_checksum_algo(algo_name); |
| 175 | info->crypto = image_get_crypto_algo(algo_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 176 | info->require_keys = require_keys; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 177 | info->engine_id = engine_id; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 178 | if (!info->checksum || !info->crypto) { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 179 | printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n", |
| 180 | algo_name, node_name, image_name); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * fit_image_process_sig- Process a single subnode of the images/ node |
| 189 | * |
| 190 | * Check each subnode and process accordingly. For signature nodes we |
| 191 | * generate a signed hash of the supplised data and store it in the node. |
| 192 | * |
| 193 | * @keydir: Directory containing keys to use for signing |
| 194 | * @keydest: Destination FDT blob to write public keys into |
| 195 | * @fit: pointer to the FIT format image header |
| 196 | * @image_name: name of image being processes (used to display errors) |
| 197 | * @noffset: subnode offset |
| 198 | * @data: data to process |
| 199 | * @size: size of data in bytes |
| 200 | * @comment: Comment to add to signature nodes |
| 201 | * @require_keys: Mark all keys as 'required' |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 202 | * @engine_id: Engine to use for signing |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 203 | * @return 0 if ok, -1 on error |
| 204 | */ |
| 205 | static int fit_image_process_sig(const char *keydir, void *keydest, |
| 206 | void *fit, const char *image_name, |
| 207 | int noffset, const void *data, size_t size, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 208 | const char *comment, int require_keys, const char *engine_id, |
| 209 | const char *cmdname) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 210 | { |
| 211 | struct image_sign_info info; |
| 212 | struct image_region region; |
| 213 | const char *node_name; |
| 214 | uint8_t *value; |
| 215 | uint value_len; |
| 216 | int ret; |
| 217 | |
| 218 | if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 219 | require_keys ? "image" : NULL, engine_id)) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 220 | return -1; |
| 221 | |
| 222 | node_name = fit_get_name(fit, noffset, NULL); |
| 223 | region.data = data; |
| 224 | region.size = size; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 225 | ret = info.crypto->sign(&info, ®ion, 1, &value, &value_len); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 226 | if (ret) { |
| 227 | printf("Failed to sign '%s' signature node in '%s' image node: %d\n", |
| 228 | node_name, image_name, ret); |
| 229 | |
| 230 | /* We allow keys to be missing */ |
| 231 | if (ret == -ENOENT) |
| 232 | return 0; |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 237 | NULL, 0, cmdname); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 238 | if (ret) { |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 239 | if (ret == -FDT_ERR_NOSPACE) |
| 240 | return -ENOSPC; |
| 241 | printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 242 | node_name, image_name, fdt_strerror(ret)); |
| 243 | return -1; |
| 244 | } |
| 245 | free(value); |
| 246 | |
| 247 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
| 248 | info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); |
| 249 | |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 250 | /* |
| 251 | * Write the public key into the supplied FDT file; this might fail |
mario.six@gdsys.cc | d019e15 | 2016-07-19 11:07:06 +0200 | [diff] [blame] | 252 | * several times, since we try signing with successively increasing |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 253 | * size values |
| 254 | */ |
Masahiro Yamada | 0148676 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 255 | if (keydest) { |
| 256 | ret = info.crypto->add_verify_data(&info, keydest); |
| 257 | if (ret) { |
| 258 | printf("Failed to add verification data for '%s' signature node in '%s' image node\n", |
| 259 | node_name, image_name); |
| 260 | return ret; |
| 261 | } |
| 262 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * fit_image_add_verification_data() - calculate/set verig. data for image node |
| 269 | * |
| 270 | * This adds hash and signature values for an component image node. |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 271 | * |
| 272 | * All existing hash subnodes are checked, if algorithm property is set to |
| 273 | * one of the supported hash algorithms, hash value is computed and |
| 274 | * corresponding hash node property is set, for example: |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 275 | * |
| 276 | * Input component image node structure: |
| 277 | * |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 278 | * o image-1 (at image_noffset) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 279 | * | - data = [binary data] |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 280 | * o hash-1 |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 281 | * |- algo = "sha1" |
| 282 | * |
| 283 | * Output component image node structure: |
| 284 | * |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 285 | * o image-1 (at image_noffset) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 286 | * | - data = [binary data] |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 287 | * o hash-1 |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 288 | * |- algo = "sha1" |
| 289 | * |- value = sha1(data) |
| 290 | * |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 291 | * For signature details, please see doc/uImage.FIT/signature.txt |
| 292 | * |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 293 | * @keydir Directory containing *.key and *.crt files (or NULL) |
| 294 | * @keydest FDT Blob to write public keys into (NULL if none) |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 295 | * @fit: Pointer to the FIT format image header |
| 296 | * @image_noffset: Requested component image node |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 297 | * @comment: Comment to add to signature nodes |
| 298 | * @require_keys: Mark all keys as 'required' |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 299 | * @engine_id: Engine to use for signing |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 300 | * @return: 0 on success, <0 on failure |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 301 | */ |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 302 | int fit_image_add_verification_data(const char *keydir, void *keydest, |
| 303 | void *fit, int image_noffset, const char *comment, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 304 | int require_keys, const char *engine_id, const char *cmdname) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 305 | { |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 306 | const char *image_name; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 307 | const void *data; |
| 308 | size_t size; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 309 | int noffset; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 310 | |
| 311 | /* Get image data and data length */ |
| 312 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
| 313 | printf("Can't get image data/size\n"); |
| 314 | return -1; |
| 315 | } |
| 316 | |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 317 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 318 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 319 | /* Process all hash subnodes of the component image node */ |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 320 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 321 | noffset >= 0; |
| 322 | noffset = fdt_next_subnode(fit, noffset)) { |
| 323 | const char *node_name; |
| 324 | int ret = 0; |
| 325 | |
| 326 | /* |
| 327 | * Check subnode name, must be equal to "hash" or "signature". |
| 328 | * Multiple hash nodes require unique unit node |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 329 | * names, e.g. hash-1, hash-2, signature-1, etc. |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 330 | */ |
| 331 | node_name = fit_get_name(fit, noffset, NULL); |
| 332 | if (!strncmp(node_name, FIT_HASH_NODENAME, |
| 333 | strlen(FIT_HASH_NODENAME))) { |
| 334 | ret = fit_image_process_hash(fit, image_name, noffset, |
| 335 | data, size); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 336 | } else if (IMAGE_ENABLE_SIGN && keydir && |
| 337 | !strncmp(node_name, FIT_SIG_NODENAME, |
| 338 | strlen(FIT_SIG_NODENAME))) { |
| 339 | ret = fit_image_process_sig(keydir, keydest, |
| 340 | fit, image_name, noffset, data, size, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 341 | comment, require_keys, engine_id, cmdname); |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 342 | } |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 343 | if (ret) |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 344 | return ret; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 350 | struct strlist { |
| 351 | int count; |
| 352 | char **strings; |
| 353 | }; |
| 354 | |
| 355 | static void strlist_init(struct strlist *list) |
| 356 | { |
| 357 | memset(list, '\0', sizeof(*list)); |
| 358 | } |
| 359 | |
| 360 | static void strlist_free(struct strlist *list) |
| 361 | { |
| 362 | int i; |
| 363 | |
| 364 | for (i = 0; i < list->count; i++) |
| 365 | free(list->strings[i]); |
| 366 | free(list->strings); |
| 367 | } |
| 368 | |
| 369 | static int strlist_add(struct strlist *list, const char *str) |
| 370 | { |
| 371 | char *dup; |
| 372 | |
| 373 | dup = strdup(str); |
| 374 | list->strings = realloc(list->strings, |
| 375 | (list->count + 1) * sizeof(char *)); |
| 376 | if (!list || !str) |
| 377 | return -1; |
| 378 | list->strings[list->count++] = dup; |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static const char *fit_config_get_image_list(void *fit, int noffset, |
| 384 | int *lenp, int *allow_missingp) |
| 385 | { |
| 386 | static const char default_list[] = FIT_KERNEL_PROP "\0" |
| 387 | FIT_FDT_PROP; |
| 388 | const char *prop; |
| 389 | |
| 390 | /* If there is an "image" property, use that */ |
| 391 | prop = fdt_getprop(fit, noffset, "sign-images", lenp); |
| 392 | if (prop) { |
| 393 | *allow_missingp = 0; |
| 394 | return *lenp ? prop : NULL; |
| 395 | } |
| 396 | |
| 397 | /* Default image list */ |
| 398 | *allow_missingp = 1; |
| 399 | *lenp = sizeof(default_list); |
| 400 | |
| 401 | return default_list; |
| 402 | } |
| 403 | |
| 404 | static int fit_config_get_hash_list(void *fit, int conf_noffset, |
| 405 | int sig_offset, struct strlist *node_inc) |
| 406 | { |
| 407 | int allow_missing; |
| 408 | const char *prop, *iname, *end; |
| 409 | const char *conf_name, *sig_name; |
| 410 | char name[200], path[200]; |
| 411 | int image_count; |
| 412 | int ret, len; |
| 413 | |
| 414 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 415 | sig_name = fit_get_name(fit, sig_offset, NULL); |
| 416 | |
| 417 | /* |
| 418 | * Build a list of nodes we need to hash. We always need the root |
| 419 | * node and the configuration. |
| 420 | */ |
| 421 | strlist_init(node_inc); |
| 422 | snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name); |
| 423 | if (strlist_add(node_inc, "/") || |
| 424 | strlist_add(node_inc, name)) |
| 425 | goto err_mem; |
| 426 | |
| 427 | /* Get a list of images that we intend to sign */ |
Heiko Schocher | fdfd520 | 2014-03-03 12:19:23 +0100 | [diff] [blame] | 428 | prop = fit_config_get_image_list(fit, sig_offset, &len, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 429 | &allow_missing); |
| 430 | if (!prop) |
| 431 | return 0; |
| 432 | |
| 433 | /* Locate the images */ |
| 434 | end = prop + len; |
| 435 | image_count = 0; |
| 436 | for (iname = prop; iname < end; iname += strlen(iname) + 1) { |
| 437 | int noffset; |
| 438 | int image_noffset; |
| 439 | int hash_count; |
| 440 | |
| 441 | image_noffset = fit_conf_get_prop_node(fit, conf_noffset, |
| 442 | iname); |
| 443 | if (image_noffset < 0) { |
| 444 | printf("Failed to find image '%s' in configuration '%s/%s'\n", |
| 445 | iname, conf_name, sig_name); |
| 446 | if (allow_missing) |
| 447 | continue; |
| 448 | |
| 449 | return -ENOENT; |
| 450 | } |
| 451 | |
| 452 | ret = fdt_get_path(fit, image_noffset, path, sizeof(path)); |
| 453 | if (ret < 0) |
| 454 | goto err_path; |
| 455 | if (strlist_add(node_inc, path)) |
| 456 | goto err_mem; |
| 457 | |
| 458 | snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, |
| 459 | conf_name); |
| 460 | |
| 461 | /* Add all this image's hashes */ |
| 462 | hash_count = 0; |
| 463 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 464 | noffset >= 0; |
| 465 | noffset = fdt_next_subnode(fit, noffset)) { |
| 466 | const char *name = fit_get_name(fit, noffset, NULL); |
| 467 | |
| 468 | if (strncmp(name, FIT_HASH_NODENAME, |
| 469 | strlen(FIT_HASH_NODENAME))) |
| 470 | continue; |
| 471 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 472 | if (ret < 0) |
| 473 | goto err_path; |
| 474 | if (strlist_add(node_inc, path)) |
| 475 | goto err_mem; |
| 476 | hash_count++; |
| 477 | } |
| 478 | |
| 479 | if (!hash_count) { |
| 480 | printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n", |
| 481 | conf_name, sig_name, iname); |
| 482 | return -ENOMSG; |
| 483 | } |
| 484 | |
| 485 | image_count++; |
| 486 | } |
| 487 | |
| 488 | if (!image_count) { |
| 489 | printf("Failed to find any images for configuration '%s/%s'\n", |
| 490 | conf_name, sig_name); |
| 491 | return -ENOMSG; |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | |
| 496 | err_mem: |
| 497 | printf("Out of memory processing configuration '%s/%s'\n", conf_name, |
| 498 | sig_name); |
| 499 | return -ENOMEM; |
| 500 | |
| 501 | err_path: |
| 502 | printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n", |
| 503 | iname, conf_name, sig_name, fdt_strerror(ret)); |
| 504 | return -ENOENT; |
| 505 | } |
| 506 | |
| 507 | static int fit_config_get_data(void *fit, int conf_noffset, int noffset, |
| 508 | struct image_region **regionp, int *region_countp, |
| 509 | char **region_propp, int *region_proplen) |
| 510 | { |
| 511 | char * const exc_prop[] = {"data"}; |
| 512 | struct strlist node_inc; |
| 513 | struct image_region *region; |
| 514 | struct fdt_region fdt_regions[100]; |
| 515 | const char *conf_name, *sig_name; |
| 516 | char path[200]; |
| 517 | int count, i; |
| 518 | char *region_prop; |
| 519 | int ret, len; |
| 520 | |
| 521 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
Masahiro Yamada | 2fc6b6f | 2017-10-19 19:16:21 +0900 | [diff] [blame] | 522 | sig_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 523 | debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name); |
| 524 | |
| 525 | /* Get a list of nodes we want to hash */ |
| 526 | ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc); |
| 527 | if (ret) |
| 528 | return ret; |
| 529 | |
| 530 | /* Get a list of regions to hash */ |
| 531 | count = fdt_find_regions(fit, node_inc.strings, node_inc.count, |
| 532 | exc_prop, ARRAY_SIZE(exc_prop), |
| 533 | fdt_regions, ARRAY_SIZE(fdt_regions), |
| 534 | path, sizeof(path), 1); |
| 535 | if (count < 0) { |
| 536 | printf("Failed to hash configuration '%s/%s': %s\n", conf_name, |
| 537 | sig_name, fdt_strerror(ret)); |
| 538 | return -EIO; |
| 539 | } |
| 540 | if (count == 0) { |
| 541 | printf("No data to hash for configuration '%s/%s': %s\n", |
| 542 | conf_name, sig_name, fdt_strerror(ret)); |
| 543 | return -EINVAL; |
| 544 | } |
| 545 | |
| 546 | /* Build our list of data blocks */ |
| 547 | region = fit_region_make_list(fit, fdt_regions, count, NULL); |
| 548 | if (!region) { |
| 549 | printf("Out of memory hashing configuration '%s/%s'\n", |
| 550 | conf_name, sig_name); |
| 551 | return -ENOMEM; |
| 552 | } |
| 553 | |
| 554 | /* Create a list of all hashed properties */ |
| 555 | debug("Hash nodes:\n"); |
| 556 | for (i = len = 0; i < node_inc.count; i++) { |
| 557 | debug(" %s\n", node_inc.strings[i]); |
| 558 | len += strlen(node_inc.strings[i]) + 1; |
| 559 | } |
| 560 | region_prop = malloc(len); |
| 561 | if (!region_prop) { |
| 562 | printf("Out of memory setting up regions for configuration '%s/%s'\n", |
| 563 | conf_name, sig_name); |
| 564 | return -ENOMEM; |
| 565 | } |
| 566 | for (i = len = 0; i < node_inc.count; |
| 567 | len += strlen(node_inc.strings[i]) + 1, i++) |
| 568 | strcpy(region_prop + len, node_inc.strings[i]); |
| 569 | strlist_free(&node_inc); |
| 570 | |
| 571 | *region_countp = count; |
| 572 | *regionp = region; |
| 573 | *region_propp = region_prop; |
| 574 | *region_proplen = len; |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static int fit_config_process_sig(const char *keydir, void *keydest, |
| 580 | void *fit, const char *conf_name, int conf_noffset, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 581 | int noffset, const char *comment, int require_keys, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 582 | const char *engine_id, const char *cmdname) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 583 | { |
| 584 | struct image_sign_info info; |
| 585 | const char *node_name; |
| 586 | struct image_region *region; |
| 587 | char *region_prop; |
| 588 | int region_proplen; |
| 589 | int region_count; |
| 590 | uint8_t *value; |
| 591 | uint value_len; |
| 592 | int ret; |
| 593 | |
| 594 | node_name = fit_get_name(fit, noffset, NULL); |
| 595 | if (fit_config_get_data(fit, conf_noffset, noffset, ®ion, |
| 596 | ®ion_count, ®ion_prop, ®ion_proplen)) |
| 597 | return -1; |
| 598 | |
| 599 | if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 600 | require_keys ? "conf" : NULL, engine_id)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 601 | return -1; |
| 602 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 603 | ret = info.crypto->sign(&info, region, region_count, &value, |
| 604 | &value_len); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 605 | free(region); |
| 606 | if (ret) { |
| 607 | printf("Failed to sign '%s' signature node in '%s' conf node\n", |
| 608 | node_name, conf_name); |
| 609 | |
| 610 | /* We allow keys to be missing */ |
| 611 | if (ret == -ENOENT) |
| 612 | return 0; |
| 613 | return -1; |
| 614 | } |
| 615 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 616 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 617 | region_prop, region_proplen, cmdname); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 618 | if (ret) { |
| 619 | if (ret == -FDT_ERR_NOSPACE) |
| 620 | return -ENOSPC; |
| 621 | printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
| 622 | node_name, conf_name, fdt_strerror(ret)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 623 | return -1; |
| 624 | } |
| 625 | free(value); |
| 626 | free(region_prop); |
| 627 | |
| 628 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
| 629 | info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); |
| 630 | |
| 631 | /* Write the public key into the supplied FDT file */ |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 632 | if (keydest) { |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 633 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 634 | if (ret) { |
Masahiro Yamada | fe391a7 | 2017-10-27 15:04:21 +0900 | [diff] [blame] | 635 | printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n", |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 636 | node_name, conf_name); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 637 | } |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 638 | return ret; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int fit_config_add_verification_data(const char *keydir, void *keydest, |
| 645 | void *fit, int conf_noffset, const char *comment, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 646 | int require_keys, const char *engine_id, const char *cmdname) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 647 | { |
| 648 | const char *conf_name; |
| 649 | int noffset; |
| 650 | |
| 651 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 652 | |
| 653 | /* Process all hash subnodes of the configuration node */ |
| 654 | for (noffset = fdt_first_subnode(fit, conf_noffset); |
| 655 | noffset >= 0; |
| 656 | noffset = fdt_next_subnode(fit, noffset)) { |
| 657 | const char *node_name; |
| 658 | int ret = 0; |
| 659 | |
| 660 | node_name = fit_get_name(fit, noffset, NULL); |
| 661 | if (!strncmp(node_name, FIT_SIG_NODENAME, |
| 662 | strlen(FIT_SIG_NODENAME))) { |
| 663 | ret = fit_config_process_sig(keydir, keydest, |
| 664 | fit, conf_name, conf_noffset, noffset, comment, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 665 | require_keys, engine_id, cmdname); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 666 | } |
| 667 | if (ret) |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 674 | int fit_add_verification_data(const char *keydir, void *keydest, void *fit, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 675 | const char *comment, int require_keys, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 676 | const char *engine_id, const char *cmdname) |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 677 | { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 678 | int images_noffset, confs_noffset; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 679 | int noffset; |
| 680 | int ret; |
| 681 | |
| 682 | /* Find images parent node offset */ |
| 683 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 684 | if (images_noffset < 0) { |
| 685 | printf("Can't find images parent node '%s' (%s)\n", |
| 686 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 687 | return images_noffset; |
| 688 | } |
| 689 | |
| 690 | /* Process its subnodes, print out component images details */ |
| 691 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 692 | noffset >= 0; |
| 693 | noffset = fdt_next_subnode(fit, noffset)) { |
| 694 | /* |
| 695 | * Direct child node of the images parent node, |
| 696 | * i.e. component image node. |
| 697 | */ |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 698 | ret = fit_image_add_verification_data(keydir, keydest, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 699 | fit, noffset, comment, require_keys, engine_id, |
| 700 | cmdname); |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 701 | if (ret) |
| 702 | return ret; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 705 | /* If there are no keys, we can't sign configurations */ |
| 706 | if (!IMAGE_ENABLE_SIGN || !keydir) |
| 707 | return 0; |
| 708 | |
| 709 | /* Find configurations parent node offset */ |
| 710 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 711 | if (confs_noffset < 0) { |
| 712 | printf("Can't find images parent node '%s' (%s)\n", |
Heiko Schocher | 441f48d | 2014-08-11 11:02:17 +0200 | [diff] [blame] | 713 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 714 | return -ENOENT; |
| 715 | } |
| 716 | |
| 717 | /* Process its subnodes, print out component images details */ |
| 718 | for (noffset = fdt_first_subnode(fit, confs_noffset); |
| 719 | noffset >= 0; |
| 720 | noffset = fdt_next_subnode(fit, noffset)) { |
| 721 | ret = fit_config_add_verification_data(keydir, keydest, |
| 722 | fit, noffset, comment, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 723 | require_keys, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 724 | engine_id, cmdname); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 725 | if (ret) |
| 726 | return ret; |
| 727 | } |
| 728 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 729 | return 0; |
| 730 | } |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 731 | |
| 732 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 733 | int fit_check_sign(const void *fit, const void *key) |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 734 | { |
| 735 | int cfg_noffset; |
| 736 | int ret; |
| 737 | |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 738 | cfg_noffset = fit_conf_get_node(fit, NULL); |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 739 | if (!cfg_noffset) |
| 740 | return -1; |
| 741 | |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 742 | printf("Verifying Hash Integrity ... "); |
| 743 | ret = fit_config_verify(fit, cfg_noffset); |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | ret = bootm_host_load_images(fit, cfg_noffset); |
| 747 | |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 748 | return ret; |
| 749 | } |
| 750 | #endif |