image-fit-sig.c 12 KB

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