image-fit-sig.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. int required_keynode, char **err_msgp)
  62. {
  63. char *algo_name;
  64. const char *padding_name;
  65. if (fdt_totalsize(fit) > CONFIG_VAL(FIT_SIGNATURE_MAX_SIZE)) {
  66. *err_msgp = "Total size too large";
  67. return 1;
  68. }
  69. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  70. *err_msgp = "Can't get hash algo property";
  71. return -1;
  72. }
  73. padding_name = fdt_getprop(fit, noffset, "padding", NULL);
  74. if (!padding_name)
  75. padding_name = RSA_DEFAULT_PADDING_NAME;
  76. memset(info, '\0', sizeof(*info));
  77. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  78. info->fit = fit;
  79. info->node_offset = noffset;
  80. info->name = algo_name;
  81. info->checksum = image_get_checksum_algo(algo_name);
  82. info->crypto = image_get_crypto_algo(algo_name);
  83. info->padding = image_get_padding_algo(padding_name);
  84. info->fdt_blob = gd_fdt_blob();
  85. info->required_keynode = required_keynode;
  86. printf("%s:%s", algo_name, info->keyname);
  87. if (!info->checksum || !info->crypto || !info->padding) {
  88. *err_msgp = "Unknown signature algorithm";
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  94. size_t size, int required_keynode, char **err_msgp)
  95. {
  96. struct image_sign_info info;
  97. struct image_region region;
  98. uint8_t *fit_value;
  99. int fit_value_len;
  100. *err_msgp = NULL;
  101. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  102. err_msgp))
  103. return -1;
  104. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  105. &fit_value_len)) {
  106. *err_msgp = "Can't get hash value property";
  107. return -1;
  108. }
  109. region.data = data;
  110. region.size = size;
  111. if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
  112. *err_msgp = "Verification failed";
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. static int fit_image_verify_sig(const void *fit, int image_noffset,
  118. const char *data, size_t size,
  119. const void *sig_blob, int sig_offset)
  120. {
  121. int noffset;
  122. char *err_msg = "";
  123. int verified = 0;
  124. int ret;
  125. /* Process all hash subnodes of the component image node */
  126. fdt_for_each_subnode(noffset, fit, image_noffset) {
  127. const char *name = fit_get_name(fit, noffset, NULL);
  128. /*
  129. * We don't support this since libfdt considers names with the
  130. * name root but different @ suffix to be equal
  131. */
  132. if (strchr(name, '@')) {
  133. err_msg = "Node name contains @";
  134. goto error;
  135. }
  136. if (!strncmp(name, FIT_SIG_NODENAME,
  137. strlen(FIT_SIG_NODENAME))) {
  138. ret = fit_image_check_sig(fit, noffset, data,
  139. size, -1, &err_msg);
  140. if (ret) {
  141. puts("- ");
  142. } else {
  143. puts("+ ");
  144. verified = 1;
  145. break;
  146. }
  147. }
  148. }
  149. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  150. err_msg = "Corrupted or truncated tree";
  151. goto error;
  152. }
  153. return verified ? 0 : -EPERM;
  154. error:
  155. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  156. err_msg, fit_get_name(fit, noffset, NULL),
  157. fit_get_name(fit, image_noffset, NULL));
  158. return -1;
  159. }
  160. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  161. const char *data, size_t size,
  162. const void *sig_blob, int *no_sigsp)
  163. {
  164. int verify_count = 0;
  165. int noffset;
  166. int sig_node;
  167. /* Work out what we need to verify */
  168. *no_sigsp = 1;
  169. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  170. if (sig_node < 0) {
  171. debug("%s: No signature node found: %s\n", __func__,
  172. fdt_strerror(sig_node));
  173. return 0;
  174. }
  175. fdt_for_each_subnode(noffset, sig_blob, sig_node) {
  176. const char *required;
  177. int ret;
  178. required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
  179. NULL);
  180. if (!required || strcmp(required, "image"))
  181. continue;
  182. ret = fit_image_verify_sig(fit, image_noffset, data, size,
  183. sig_blob, noffset);
  184. if (ret) {
  185. printf("Failed to verify required signature '%s'\n",
  186. fit_get_name(sig_blob, noffset, NULL));
  187. return ret;
  188. }
  189. verify_count++;
  190. }
  191. if (verify_count)
  192. *no_sigsp = 0;
  193. return 0;
  194. }
  195. /**
  196. * fit_config_check_sig() - Check the signature of a config
  197. *
  198. * @fit: FIT to check
  199. * @noffset: Offset of configuration node (e.g. /configurations/conf-1)
  200. * @required_keynode: Offset in the control FDT of the required key node,
  201. * if any. If this is given, then the configuration wil not
  202. * pass verification unless that key is used. If this is
  203. * -1 then any signature will do.
  204. * @conf_noffset: Offset of the configuration subnode being checked (e.g.
  205. * /configurations/conf-1/kernel)
  206. * @err_msgp: In the event of an error, this will be pointed to a
  207. * help error string to display to the user.
  208. * @return 0 if all verified ok, <0 on error
  209. */
  210. static int fit_config_check_sig(const void *fit, int noffset,
  211. int required_keynode, int conf_noffset,
  212. char **err_msgp)
  213. {
  214. static char * const exc_prop[] = {
  215. "data",
  216. "data-size",
  217. "data-position",
  218. "data-offset"
  219. };
  220. const char *prop, *end, *name;
  221. struct image_sign_info info;
  222. const uint32_t *strings;
  223. const char *config_name;
  224. uint8_t *fit_value;
  225. int fit_value_len;
  226. bool found_config;
  227. int max_regions;
  228. int i, prop_len;
  229. char path[200];
  230. int count;
  231. config_name = fit_get_name(fit, conf_noffset, NULL);
  232. debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
  233. fit_get_name(fit, noffset, NULL),
  234. fit_get_name(gd_fdt_blob(), required_keynode, NULL));
  235. *err_msgp = NULL;
  236. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  237. err_msgp))
  238. return -1;
  239. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  240. &fit_value_len)) {
  241. *err_msgp = "Can't get hash value property";
  242. return -1;
  243. }
  244. /* Count the number of strings in the property */
  245. prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
  246. end = prop ? prop + prop_len : prop;
  247. for (name = prop, count = 0; name < end; name++)
  248. if (!*name)
  249. count++;
  250. if (!count) {
  251. *err_msgp = "Can't get hashed-nodes property";
  252. return -1;
  253. }
  254. if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
  255. *err_msgp = "hashed-nodes property must be null-terminated";
  256. return -1;
  257. }
  258. /* Add a sanity check here since we are using the stack */
  259. if (count > IMAGE_MAX_HASHED_NODES) {
  260. *err_msgp = "Number of hashed nodes exceeds maximum";
  261. return -1;
  262. }
  263. /* Create a list of node names from those strings */
  264. char *node_inc[count];
  265. debug("Hash nodes (%d):\n", count);
  266. found_config = false;
  267. for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
  268. debug(" '%s'\n", name);
  269. node_inc[i] = (char *)name;
  270. if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
  271. name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
  272. !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
  273. debug(" (found config node %s)", config_name);
  274. found_config = true;
  275. }
  276. }
  277. if (!found_config) {
  278. *err_msgp = "Selected config not in hashed nodes";
  279. return -1;
  280. }
  281. /*
  282. * Each node can generate one region for each sub-node. Allow for
  283. * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
  284. */
  285. max_regions = 20 + count * 7;
  286. struct fdt_region fdt_regions[max_regions];
  287. /* Get a list of regions to hash */
  288. count = fdt_find_regions(fit, node_inc, count,
  289. exc_prop, ARRAY_SIZE(exc_prop),
  290. fdt_regions, max_regions - 1,
  291. path, sizeof(path), 0);
  292. if (count < 0) {
  293. *err_msgp = "Failed to hash configuration";
  294. return -1;
  295. }
  296. if (count == 0) {
  297. *err_msgp = "No data to hash";
  298. return -1;
  299. }
  300. if (count >= max_regions - 1) {
  301. *err_msgp = "Too many hash regions";
  302. return -1;
  303. }
  304. /* Add the strings */
  305. strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
  306. if (strings) {
  307. /*
  308. * The strings region offset must be a static 0x0.
  309. * This is set in tool/image-host.c
  310. */
  311. fdt_regions[count].offset = fdt_off_dt_strings(fit);
  312. fdt_regions[count].size = fdt32_to_cpu(strings[1]);
  313. count++;
  314. }
  315. /* Allocate the region list on the stack */
  316. struct image_region region[count];
  317. fit_region_make_list(fit, fdt_regions, count, region);
  318. if (info.crypto->verify(&info, region, count, fit_value,
  319. fit_value_len)) {
  320. *err_msgp = "Verification failed";
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. static int fit_config_verify_sig(const void *fit, int conf_noffset,
  326. const void *sig_blob, int sig_offset)
  327. {
  328. int noffset;
  329. char *err_msg = "No 'signature' subnode found";
  330. int verified = 0;
  331. int ret;
  332. /* Process all hash subnodes of the component conf node */
  333. fdt_for_each_subnode(noffset, fit, conf_noffset) {
  334. const char *name = fit_get_name(fit, noffset, NULL);
  335. if (!strncmp(name, FIT_SIG_NODENAME,
  336. strlen(FIT_SIG_NODENAME))) {
  337. ret = fit_config_check_sig(fit, noffset, sig_offset,
  338. conf_noffset, &err_msg);
  339. if (ret) {
  340. puts("- ");
  341. } else {
  342. puts("+ ");
  343. verified = 1;
  344. break;
  345. }
  346. }
  347. }
  348. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  349. err_msg = "Corrupted or truncated tree";
  350. goto error;
  351. }
  352. if (verified)
  353. return 0;
  354. error:
  355. printf(" error!\n%s for '%s' hash node in '%s' config node\n",
  356. err_msg, fit_get_name(fit, noffset, NULL),
  357. fit_get_name(fit, conf_noffset, NULL));
  358. return -EPERM;
  359. }
  360. static int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
  361. const void *sig_blob)
  362. {
  363. const char *name = fit_get_name(fit, conf_noffset, NULL);
  364. int noffset;
  365. int sig_node;
  366. int verified = 0;
  367. int reqd_sigs = 0;
  368. bool reqd_policy_all = true;
  369. const char *reqd_mode;
  370. /*
  371. * We don't support this since libfdt considers names with the
  372. * name root but different @ suffix to be equal
  373. */
  374. if (strchr(name, '@')) {
  375. printf("Configuration node '%s' contains '@'\n", name);
  376. return -EPERM;
  377. }
  378. /* Work out what we need to verify */
  379. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  380. if (sig_node < 0) {
  381. debug("%s: No signature node found: %s\n", __func__,
  382. fdt_strerror(sig_node));
  383. return 0;
  384. }
  385. /* Get required-mode policy property from DTB */
  386. reqd_mode = fdt_getprop(sig_blob, sig_node, "required-mode", NULL);
  387. if (reqd_mode && !strcmp(reqd_mode, "any"))
  388. reqd_policy_all = false;
  389. debug("%s: required-mode policy set to '%s'\n", __func__,
  390. reqd_policy_all ? "all" : "any");
  391. fdt_for_each_subnode(noffset, sig_blob, sig_node) {
  392. const char *required;
  393. int ret;
  394. required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
  395. NULL);
  396. if (!required || strcmp(required, "conf"))
  397. continue;
  398. reqd_sigs++;
  399. ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
  400. noffset);
  401. if (ret) {
  402. if (reqd_policy_all) {
  403. printf("Failed to verify required signature '%s'\n",
  404. fit_get_name(sig_blob, noffset, NULL));
  405. return ret;
  406. }
  407. } else {
  408. verified++;
  409. if (!reqd_policy_all)
  410. break;
  411. }
  412. }
  413. if (reqd_sigs && !verified) {
  414. printf("Failed to verify 'any' of the required signature(s)\n");
  415. return -EPERM;
  416. }
  417. return 0;
  418. }
  419. int fit_config_verify(const void *fit, int conf_noffset)
  420. {
  421. return fit_config_verify_required_sigs(fit, conf_noffset,
  422. gd_fdt_blob());
  423. }