image-fit-sig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 <malloc.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. #endif /* !USE_HOSTCC*/
  13. #include <image.h>
  14. #include <u-boot/rsa.h>
  15. #include <u-boot/rsa-checksum.h>
  16. #define IMAGE_MAX_HASHED_NODES 100
  17. #ifdef USE_HOSTCC
  18. void *host_blob;
  19. void image_set_host_blob(void *blob)
  20. {
  21. host_blob = blob;
  22. }
  23. void *image_get_host_blob(void)
  24. {
  25. return host_blob;
  26. }
  27. #endif
  28. /**
  29. * fit_region_make_list() - Make a list of image regions
  30. *
  31. * Given a list of fdt_regions, create a list of image_regions. This is a
  32. * simple conversion routine since the FDT and image code use different
  33. * structures.
  34. *
  35. * @fit: FIT image
  36. * @fdt_regions: Pointer to FDT regions
  37. * @count: Number of FDT regions
  38. * @region: Pointer to image regions, which must hold @count records. If
  39. * region is NULL, then (except for an SPL build) the array will be
  40. * allocated.
  41. * @return: Pointer to image regions
  42. */
  43. struct image_region *fit_region_make_list(const void *fit,
  44. struct fdt_region *fdt_regions,
  45. int count,
  46. struct image_region *region)
  47. {
  48. int i;
  49. debug("Hash regions:\n");
  50. debug("%10s %10s\n", "Offset", "Size");
  51. /*
  52. * Use malloc() except in SPL (to save code size). In SPL the caller
  53. * must allocate the array.
  54. */
  55. #ifndef CONFIG_SPL_BUILD
  56. if (!region)
  57. region = calloc(sizeof(*region), count);
  58. #endif
  59. if (!region)
  60. return NULL;
  61. for (i = 0; i < count; i++) {
  62. debug("%10x %10x\n", fdt_regions[i].offset,
  63. fdt_regions[i].size);
  64. region[i].data = fit + fdt_regions[i].offset;
  65. region[i].size = fdt_regions[i].size;
  66. }
  67. return region;
  68. }
  69. static int fit_image_setup_verify(struct image_sign_info *info,
  70. const void *fit, int noffset,
  71. int required_keynode, char **err_msgp)
  72. {
  73. char *algo_name;
  74. const char *padding_name;
  75. if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
  76. *err_msgp = "Total size too large";
  77. return 1;
  78. }
  79. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  80. *err_msgp = "Can't get hash algo property";
  81. return -1;
  82. }
  83. padding_name = fdt_getprop(fit, noffset, "padding", NULL);
  84. if (!padding_name)
  85. padding_name = RSA_DEFAULT_PADDING_NAME;
  86. memset(info, '\0', sizeof(*info));
  87. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  88. info->fit = (void *)fit;
  89. info->node_offset = noffset;
  90. info->name = algo_name;
  91. info->checksum = image_get_checksum_algo(algo_name);
  92. info->crypto = image_get_crypto_algo(algo_name);
  93. info->padding = image_get_padding_algo(padding_name);
  94. info->fdt_blob = gd_fdt_blob();
  95. info->required_keynode = required_keynode;
  96. printf("%s:%s", algo_name, info->keyname);
  97. if (!info->checksum || !info->crypto || !info->padding) {
  98. *err_msgp = "Unknown signature algorithm";
  99. return -1;
  100. }
  101. return 0;
  102. }
  103. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  104. size_t size, int required_keynode, char **err_msgp)
  105. {
  106. struct image_sign_info info;
  107. struct image_region region;
  108. uint8_t *fit_value;
  109. int fit_value_len;
  110. *err_msgp = NULL;
  111. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  112. err_msgp))
  113. return -1;
  114. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  115. &fit_value_len)) {
  116. *err_msgp = "Can't get hash value property";
  117. return -1;
  118. }
  119. region.data = data;
  120. region.size = size;
  121. if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
  122. *err_msgp = "Verification failed";
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. static int fit_image_verify_sig(const void *fit, int image_noffset,
  128. const char *data, size_t size,
  129. const void *sig_blob, int sig_offset)
  130. {
  131. int noffset;
  132. char *err_msg = "";
  133. int verified = 0;
  134. int ret;
  135. /* Process all hash subnodes of the component image node */
  136. fdt_for_each_subnode(noffset, fit, image_noffset) {
  137. const char *name = fit_get_name(fit, noffset, NULL);
  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. char * const exc_prop[] = {"data"};
  217. const char *prop, *end, *name;
  218. struct image_sign_info info;
  219. const uint32_t *strings;
  220. const char *config_name;
  221. uint8_t *fit_value;
  222. int fit_value_len;
  223. bool found_config;
  224. int max_regions;
  225. int i, prop_len;
  226. char path[200];
  227. int count;
  228. config_name = fit_get_name(fit, conf_noffset, NULL);
  229. debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
  230. fit_get_name(fit, noffset, NULL),
  231. fit_get_name(gd_fdt_blob(), required_keynode, NULL));
  232. *err_msgp = NULL;
  233. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  234. err_msgp))
  235. return -1;
  236. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  237. &fit_value_len)) {
  238. *err_msgp = "Can't get hash value property";
  239. return -1;
  240. }
  241. /* Count the number of strings in the property */
  242. prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
  243. end = prop ? prop + prop_len : prop;
  244. for (name = prop, count = 0; name < end; name++)
  245. if (!*name)
  246. count++;
  247. if (!count) {
  248. *err_msgp = "Can't get hashed-nodes property";
  249. return -1;
  250. }
  251. if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
  252. *err_msgp = "hashed-nodes property must be null-terminated";
  253. return -1;
  254. }
  255. /* Add a sanity check here since we are using the stack */
  256. if (count > IMAGE_MAX_HASHED_NODES) {
  257. *err_msgp = "Number of hashed nodes exceeds maximum";
  258. return -1;
  259. }
  260. /* Create a list of node names from those strings */
  261. char *node_inc[count];
  262. debug("Hash nodes (%d):\n", count);
  263. found_config = false;
  264. for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
  265. debug(" '%s'\n", name);
  266. node_inc[i] = (char *)name;
  267. if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
  268. name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
  269. !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
  270. debug(" (found config node %s)", config_name);
  271. found_config = true;
  272. }
  273. }
  274. if (!found_config) {
  275. *err_msgp = "Selected config not in hashed nodes";
  276. return -1;
  277. }
  278. /*
  279. * Each node can generate one region for each sub-node. Allow for
  280. * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
  281. */
  282. max_regions = 20 + count * 7;
  283. struct fdt_region fdt_regions[max_regions];
  284. /* Get a list of regions to hash */
  285. count = fdt_find_regions(fit, node_inc, count,
  286. exc_prop, ARRAY_SIZE(exc_prop),
  287. fdt_regions, max_regions - 1,
  288. path, sizeof(path), 0);
  289. if (count < 0) {
  290. *err_msgp = "Failed to hash configuration";
  291. return -1;
  292. }
  293. if (count == 0) {
  294. *err_msgp = "No data to hash";
  295. return -1;
  296. }
  297. if (count >= max_regions - 1) {
  298. *err_msgp = "Too many hash regions";
  299. return -1;
  300. }
  301. /* Add the strings */
  302. strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
  303. if (strings) {
  304. /*
  305. * The strings region offset must be a static 0x0.
  306. * This is set in tool/image-host.c
  307. */
  308. fdt_regions[count].offset = fdt_off_dt_strings(fit);
  309. fdt_regions[count].size = fdt32_to_cpu(strings[1]);
  310. count++;
  311. }
  312. /* Allocate the region list on the stack */
  313. struct image_region region[count];
  314. fit_region_make_list(fit, fdt_regions, count, region);
  315. if (info.crypto->verify(&info, region, count, fit_value,
  316. fit_value_len)) {
  317. *err_msgp = "Verification failed";
  318. return -1;
  319. }
  320. return 0;
  321. }
  322. static int fit_config_verify_sig(const void *fit, int conf_noffset,
  323. const void *sig_blob, int sig_offset)
  324. {
  325. int noffset;
  326. char *err_msg = "";
  327. int verified = 0;
  328. int ret;
  329. /* Process all hash subnodes of the component conf node */
  330. fdt_for_each_subnode(noffset, fit, conf_noffset) {
  331. const char *name = fit_get_name(fit, noffset, NULL);
  332. if (!strncmp(name, FIT_SIG_NODENAME,
  333. strlen(FIT_SIG_NODENAME))) {
  334. ret = fit_config_check_sig(fit, noffset, sig_offset,
  335. conf_noffset, &err_msg);
  336. if (ret) {
  337. puts("- ");
  338. } else {
  339. puts("+ ");
  340. verified = 1;
  341. break;
  342. }
  343. }
  344. }
  345. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  346. err_msg = "Corrupted or truncated tree";
  347. goto error;
  348. }
  349. if (verified)
  350. return 0;
  351. error:
  352. printf(" error!\n%s for '%s' hash node in '%s' config node\n",
  353. err_msg, fit_get_name(fit, noffset, NULL),
  354. fit_get_name(fit, conf_noffset, NULL));
  355. return -EPERM;
  356. }
  357. int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
  358. const void *sig_blob)
  359. {
  360. int noffset;
  361. int sig_node;
  362. /* Work out what we need to verify */
  363. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  364. if (sig_node < 0) {
  365. debug("%s: No signature node found: %s\n", __func__,
  366. fdt_strerror(sig_node));
  367. return 0;
  368. }
  369. fdt_for_each_subnode(noffset, sig_blob, sig_node) {
  370. const char *required;
  371. int ret;
  372. required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
  373. NULL);
  374. if (!required || strcmp(required, "conf"))
  375. continue;
  376. ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
  377. noffset);
  378. if (ret) {
  379. printf("Failed to verify required signature '%s'\n",
  380. fit_get_name(sig_blob, noffset, NULL));
  381. return ret;
  382. }
  383. }
  384. return 0;
  385. }
  386. int fit_config_verify(const void *fit, int conf_noffset)
  387. {
  388. return fit_config_verify_required_sigs(fit, conf_noffset,
  389. gd_fdt_blob());
  390. }