image-fit-sig.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. */
  5. #ifdef USE_HOSTCC
  6. #include "mkimage.h"
  7. #include <time.h>
  8. #else
  9. #include <common.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <asm/global_data.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. #endif /* !USE_HOSTCC*/
  15. #include <fdt_region.h>
  16. #include <image.h>
  17. #include <u-boot/rsa.h>
  18. #include <u-boot/hash-checksum.h>
  19. #define IMAGE_MAX_HASHED_NODES 100
  20. /**
  21. * fit_region_make_list() - Make a list of image regions
  22. *
  23. * Given a list of fdt_regions, create a list of image_regions. This is a
  24. * simple conversion routine since the FDT and image code use different
  25. * structures.
  26. *
  27. * @fit: FIT image
  28. * @fdt_regions: Pointer to FDT regions
  29. * @count: Number of FDT regions
  30. * @region: Pointer to image regions, which must hold @count records. If
  31. * region is NULL, then (except for an SPL build) the array will be
  32. * allocated.
  33. * @return: Pointer to image regions
  34. */
  35. struct image_region *fit_region_make_list(const void *fit,
  36. struct fdt_region *fdt_regions,
  37. int count,
  38. struct image_region *region)
  39. {
  40. int i;
  41. debug("Hash regions:\n");
  42. debug("%10s %10s\n", "Offset", "Size");
  43. /*
  44. * Use malloc() except in SPL (to save code size). In SPL the caller
  45. * must allocate the array.
  46. */
  47. if (!IS_ENABLED(CONFIG_SPL_BUILD) && !region)
  48. region = calloc(sizeof(*region), count);
  49. if (!region)
  50. return NULL;
  51. for (i = 0; i < count; i++) {
  52. debug("%10x %10x\n", fdt_regions[i].offset,
  53. fdt_regions[i].size);
  54. region[i].data = fit + fdt_regions[i].offset;
  55. region[i].size = fdt_regions[i].size;
  56. }
  57. return region;
  58. }
  59. static int fit_image_setup_verify(struct image_sign_info *info,
  60. const void *fit, int noffset,
  61. const void *key_blob, int required_keynode,
  62. char **err_msgp)
  63. {
  64. const char *algo_name;
  65. const char *padding_name;
  66. if (fdt_totalsize(fit) > CONFIG_VAL(FIT_SIGNATURE_MAX_SIZE)) {
  67. *err_msgp = "Total size too large";
  68. return 1;
  69. }
  70. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  71. *err_msgp = "Can't get hash algo property";
  72. return -1;
  73. }
  74. padding_name = fdt_getprop(fit, noffset, "padding", NULL);
  75. if (!padding_name)
  76. padding_name = RSA_DEFAULT_PADDING_NAME;
  77. memset(info, '\0', sizeof(*info));
  78. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  79. info->fit = fit;
  80. info->node_offset = noffset;
  81. info->name = algo_name;
  82. info->checksum = image_get_checksum_algo(algo_name);
  83. info->crypto = image_get_crypto_algo(algo_name);
  84. info->padding = image_get_padding_algo(padding_name);
  85. info->fdt_blob = key_blob;
  86. info->required_keynode = required_keynode;
  87. printf("%s:%s", algo_name, info->keyname);
  88. if (!info->checksum || !info->crypto || !info->padding) {
  89. *err_msgp = "Unknown signature algorithm";
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  95. size_t size, const void *key_blob, int required_keynode,
  96. char **err_msgp)
  97. {
  98. struct image_sign_info info;
  99. struct image_region region;
  100. uint8_t *fit_value;
  101. int fit_value_len;
  102. *err_msgp = NULL;
  103. if (fit_image_setup_verify(&info, fit, noffset, key_blob,
  104. required_keynode, err_msgp))
  105. return -1;
  106. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  107. &fit_value_len)) {
  108. *err_msgp = "Can't get hash value property";
  109. return -1;
  110. }
  111. region.data = data;
  112. region.size = size;
  113. if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
  114. *err_msgp = "Verification failed";
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. static int fit_image_verify_sig(const void *fit, int image_noffset,
  120. const char *data, size_t size,
  121. const void *key_blob, int key_offset)
  122. {
  123. int noffset;
  124. char *err_msg = "";
  125. int verified = 0;
  126. int ret;
  127. /* Process all hash subnodes of the component image node */
  128. fdt_for_each_subnode(noffset, fit, image_noffset) {
  129. const char *name = fit_get_name(fit, noffset, NULL);
  130. /*
  131. * We don't support this since libfdt considers names with the
  132. * name root but different @ suffix to be equal
  133. */
  134. if (strchr(name, '@')) {
  135. err_msg = "Node name contains @";
  136. goto error;
  137. }
  138. if (!strncmp(name, FIT_SIG_NODENAME,
  139. strlen(FIT_SIG_NODENAME))) {
  140. ret = fit_image_check_sig(fit, noffset, data, size,
  141. key_blob, -1, &err_msg);
  142. if (ret) {
  143. puts("- ");
  144. } else {
  145. puts("+ ");
  146. verified = 1;
  147. break;
  148. }
  149. }
  150. }
  151. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  152. err_msg = "Corrupted or truncated tree";
  153. goto error;
  154. }
  155. return verified ? 0 : -EPERM;
  156. error:
  157. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  158. err_msg, fit_get_name(fit, noffset, NULL),
  159. fit_get_name(fit, image_noffset, NULL));
  160. return -1;
  161. }
  162. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  163. const char *data, size_t size,
  164. const void *key_blob, int *no_sigsp)
  165. {
  166. int verify_count = 0;
  167. int noffset;
  168. int key_node;
  169. /* Work out what we need to verify */
  170. *no_sigsp = 1;
  171. key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
  172. if (key_node < 0) {
  173. debug("%s: No signature node found: %s\n", __func__,
  174. fdt_strerror(key_node));
  175. return 0;
  176. }
  177. fdt_for_each_subnode(noffset, key_blob, key_node) {
  178. const char *required;
  179. int ret;
  180. required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
  181. NULL);
  182. if (!required || strcmp(required, "image"))
  183. continue;
  184. ret = fit_image_verify_sig(fit, image_noffset, data, size,
  185. key_blob, noffset);
  186. if (ret) {
  187. printf("Failed to verify required signature '%s'\n",
  188. fit_get_name(key_blob, noffset, NULL));
  189. return ret;
  190. }
  191. verify_count++;
  192. }
  193. if (verify_count)
  194. *no_sigsp = 0;
  195. return 0;
  196. }
  197. /**
  198. * fit_config_check_sig() - Check the signature of a config
  199. *
  200. * Here we are looking at a particular signature that needs verification (here
  201. * signature-1):
  202. *
  203. * configurations {
  204. * default = "conf-1";
  205. * conf-1 {
  206. * kernel = "kernel-1";
  207. * fdt = "fdt-1";
  208. * signature-1 {
  209. * algo = "sha1,rsa2048";
  210. * value = <...conf 1 signature...>;
  211. * };
  212. * };
  213. *
  214. * @fit: FIT to check
  215. * @noffset: Offset of the signature node being checked (e.g.
  216. * /configurations/conf-1/signature-1)
  217. * @conf_noffset: Offset of configuration node (e.g. /configurations/conf-1)
  218. * @key_blob: Blob containing the keys to check against
  219. * @required_keynode: Offset in @key_blob of the required key node,
  220. * if any. If this is given, then the configuration wil not
  221. * pass verification unless that key is used. If this is
  222. * -1 then any signature will do.
  223. * @err_msgp: In the event of an error, this will be pointed to a
  224. * help error string to display to the user.
  225. * Return: 0 if all verified ok, <0 on error
  226. */
  227. static int fit_config_check_sig(const void *fit, int noffset, int conf_noffset,
  228. const void *key_blob, int required_keynode,
  229. char **err_msgp)
  230. {
  231. static char * const exc_prop[] = {
  232. "data",
  233. "data-size",
  234. "data-position",
  235. "data-offset"
  236. };
  237. const char *prop, *end, *name;
  238. struct image_sign_info info;
  239. const uint32_t *strings;
  240. const char *config_name;
  241. uint8_t *fit_value;
  242. int fit_value_len;
  243. bool found_config;
  244. int max_regions;
  245. int i, prop_len;
  246. char path[200];
  247. int count;
  248. config_name = fit_get_name(fit, conf_noffset, NULL);
  249. debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, key_blob,
  250. fit_get_name(fit, noffset, NULL),
  251. fit_get_name(key_blob, required_keynode, NULL));
  252. *err_msgp = NULL;
  253. if (fit_image_setup_verify(&info, fit, noffset, key_blob,
  254. required_keynode, err_msgp))
  255. return -1;
  256. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  257. &fit_value_len)) {
  258. *err_msgp = "Can't get hash value property";
  259. return -1;
  260. }
  261. /* Count the number of strings in the property */
  262. prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
  263. end = prop ? prop + prop_len : prop;
  264. for (name = prop, count = 0; name < end; name++)
  265. if (!*name)
  266. count++;
  267. if (!count) {
  268. *err_msgp = "Can't get hashed-nodes property";
  269. return -1;
  270. }
  271. if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
  272. *err_msgp = "hashed-nodes property must be null-terminated";
  273. return -1;
  274. }
  275. /* Add a sanity check here since we are using the stack */
  276. if (count > IMAGE_MAX_HASHED_NODES) {
  277. *err_msgp = "Number of hashed nodes exceeds maximum";
  278. return -1;
  279. }
  280. /* Create a list of node names from those strings */
  281. char *node_inc[count];
  282. debug("Hash nodes (%d):\n", count);
  283. found_config = false;
  284. for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
  285. debug(" '%s'\n", name);
  286. node_inc[i] = (char *)name;
  287. if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
  288. name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
  289. !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
  290. debug(" (found config node %s)", config_name);
  291. found_config = true;
  292. }
  293. }
  294. if (!found_config) {
  295. *err_msgp = "Selected config not in hashed nodes";
  296. return -1;
  297. }
  298. /*
  299. * Each node can generate one region for each sub-node. Allow for
  300. * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
  301. */
  302. max_regions = 20 + count * 7;
  303. struct fdt_region fdt_regions[max_regions];
  304. /* Get a list of regions to hash */
  305. count = fdt_find_regions(fit, node_inc, count,
  306. exc_prop, ARRAY_SIZE(exc_prop),
  307. fdt_regions, max_regions - 1,
  308. path, sizeof(path), 0);
  309. if (count < 0) {
  310. *err_msgp = "Failed to hash configuration";
  311. return -1;
  312. }
  313. if (count == 0) {
  314. *err_msgp = "No data to hash";
  315. return -1;
  316. }
  317. if (count >= max_regions - 1) {
  318. *err_msgp = "Too many hash regions";
  319. return -1;
  320. }
  321. /* Add the strings */
  322. strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
  323. if (strings) {
  324. /*
  325. * The strings region offset must be a static 0x0.
  326. * This is set in tool/image-host.c
  327. */
  328. fdt_regions[count].offset = fdt_off_dt_strings(fit);
  329. fdt_regions[count].size = fdt32_to_cpu(strings[1]);
  330. count++;
  331. }
  332. /* Allocate the region list on the stack */
  333. struct image_region region[count];
  334. fit_region_make_list(fit, fdt_regions, count, region);
  335. if (info.crypto->verify(&info, region, count, fit_value,
  336. fit_value_len)) {
  337. *err_msgp = "Verification failed";
  338. return -1;
  339. }
  340. return 0;
  341. }
  342. /**
  343. * fit_config_verify_key() - Verify that a configuration is signed with a key
  344. *
  345. * Here we are looking at a particular configuration that needs verification:
  346. *
  347. * configurations {
  348. * default = "conf-1";
  349. * conf-1 {
  350. * kernel = "kernel-1";
  351. * fdt = "fdt-1";
  352. * signature-1 {
  353. * algo = "sha1,rsa2048";
  354. * value = <...conf 1 signature...>;
  355. * };
  356. * };
  357. *
  358. * We must check each of the signature subnodes of conf-1. Hopefully one of them
  359. * will match the key at key_offset.
  360. *
  361. * @fit: FIT to check
  362. * @conf_noffset: Offset of the configuration node to check (e.g.
  363. * /configurations/conf-1)
  364. * @key_blob: Blob containing the keys to check against
  365. * @key_offset: Offset of the key to check within @key_blob
  366. * @return 0 if OK, -EPERM if any signatures did not verify, or the
  367. * configuration node has an invalid name
  368. */
  369. static int fit_config_verify_key(const void *fit, int conf_noffset,
  370. const void *key_blob, int key_offset)
  371. {
  372. int noffset;
  373. char *err_msg = "No 'signature' subnode found";
  374. int verified = 0;
  375. int ret;
  376. /* Process all hash subnodes of the component conf node */
  377. fdt_for_each_subnode(noffset, fit, conf_noffset) {
  378. const char *name = fit_get_name(fit, noffset, NULL);
  379. if (!strncmp(name, FIT_SIG_NODENAME,
  380. strlen(FIT_SIG_NODENAME))) {
  381. ret = fit_config_check_sig(fit, noffset, conf_noffset,
  382. key_blob, key_offset,
  383. &err_msg);
  384. if (ret) {
  385. puts("- ");
  386. } else {
  387. puts("+ ");
  388. verified = 1;
  389. break;
  390. }
  391. }
  392. }
  393. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  394. err_msg = "Corrupted or truncated tree";
  395. goto error;
  396. }
  397. if (verified)
  398. return 0;
  399. error:
  400. printf(" error!\n%s for '%s' hash node in '%s' config node\n",
  401. err_msg, fit_get_name(fit, noffset, NULL),
  402. fit_get_name(fit, conf_noffset, NULL));
  403. return -EPERM;
  404. }
  405. /**
  406. * fit_config_verify_required_keys() - verify any required signatures for config
  407. *
  408. * This looks through all the signatures we expect and verifies that at least
  409. * all the required ones are valid signatures for the configuration
  410. *
  411. * @fit: FIT to check
  412. * @conf_noffset: Offset of the configuration node to check (e.g.
  413. * /configurations/conf-1)
  414. * @key_blob: Blob containing the keys to check against
  415. * @return 0 if OK, -EPERM if any signatures did not verify, or the
  416. * configuration node has an invalid name
  417. */
  418. static int fit_config_verify_required_keys(const void *fit, int conf_noffset,
  419. const void *key_blob)
  420. {
  421. const char *name = fit_get_name(fit, conf_noffset, NULL);
  422. int noffset;
  423. int key_node;
  424. int verified = 0;
  425. int reqd_sigs = 0;
  426. bool reqd_policy_all = true;
  427. const char *reqd_mode;
  428. /*
  429. * We don't support this since libfdt considers names with the
  430. * name root but different @ suffix to be equal
  431. */
  432. if (strchr(name, '@')) {
  433. printf("Configuration node '%s' contains '@'\n", name);
  434. return -EPERM;
  435. }
  436. /* Work out what we need to verify */
  437. key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
  438. if (key_node < 0) {
  439. debug("%s: No signature node found: %s\n", __func__,
  440. fdt_strerror(key_node));
  441. return 0;
  442. }
  443. /* Get required-mode policy property from DTB */
  444. reqd_mode = fdt_getprop(key_blob, key_node, "required-mode", NULL);
  445. if (reqd_mode && !strcmp(reqd_mode, "any"))
  446. reqd_policy_all = false;
  447. debug("%s: required-mode policy set to '%s'\n", __func__,
  448. reqd_policy_all ? "all" : "any");
  449. /*
  450. * The algorithm here is a little convoluted due to how we want it to
  451. * work. Here we work through each of the signature nodes in the
  452. * public-key area. These are in the U-Boot control devicetree. Each
  453. * node was created by signing a configuration, so we check if it is
  454. * 'required' and if so, request that it be verified.
  455. */
  456. fdt_for_each_subnode(noffset, key_blob, key_node) {
  457. const char *required;
  458. int ret;
  459. required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
  460. NULL);
  461. if (!required || strcmp(required, "conf"))
  462. continue;
  463. reqd_sigs++;
  464. ret = fit_config_verify_key(fit, conf_noffset, key_blob,
  465. noffset);
  466. if (ret) {
  467. if (reqd_policy_all) {
  468. printf("Failed to verify required signature '%s'\n",
  469. fit_get_name(key_blob, noffset, NULL));
  470. return ret;
  471. }
  472. } else {
  473. verified++;
  474. if (!reqd_policy_all)
  475. break;
  476. }
  477. }
  478. if (reqd_sigs && !verified) {
  479. printf("Failed to verify 'any' of the required signature(s)\n");
  480. return -EPERM;
  481. }
  482. return 0;
  483. }
  484. int fit_config_verify(const void *fit, int conf_noffset)
  485. {
  486. return fit_config_verify_required_keys(fit, conf_noffset,
  487. gd_fdt_blob());
  488. }