image-sig.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (c) 2013, Google Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #ifdef USE_HOSTCC
  7. #include "mkimage.h"
  8. #include <time.h>
  9. #else
  10. #include <common.h>
  11. #include <malloc.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #endif /* !USE_HOSTCC*/
  14. #include <image.h>
  15. #include <rsa.h>
  16. #define IMAGE_MAX_HASHED_NODES 100
  17. struct image_sig_algo image_sig_algos[] = {
  18. {
  19. "sha1,rsa2048",
  20. rsa_sign,
  21. rsa_add_verify_data,
  22. rsa_verify,
  23. }
  24. };
  25. struct image_sig_algo *image_get_sig_algo(const char *name)
  26. {
  27. int i;
  28. for (i = 0; i < ARRAY_SIZE(image_sig_algos); i++) {
  29. if (!strcmp(image_sig_algos[i].name, name))
  30. return &image_sig_algos[i];
  31. }
  32. return NULL;
  33. }
  34. /**
  35. * fit_region_make_list() - Make a list of image regions
  36. *
  37. * Given a list of fdt_regions, create a list of image_regions. This is a
  38. * simple conversion routine since the FDT and image code use different
  39. * structures.
  40. *
  41. * @fit: FIT image
  42. * @fdt_regions: Pointer to FDT regions
  43. * @count: Number of FDT regions
  44. * @region: Pointer to image regions, which must hold @count records. If
  45. * region is NULL, then (except for an SPL build) the array will be
  46. * allocated.
  47. * @return: Pointer to image regions
  48. */
  49. struct image_region *fit_region_make_list(const void *fit,
  50. struct fdt_region *fdt_regions, int count,
  51. struct image_region *region)
  52. {
  53. int i;
  54. debug("Hash regions:\n");
  55. debug("%10s %10s\n", "Offset", "Size");
  56. /*
  57. * Use malloc() except in SPL (to save code size). In SPL the caller
  58. * must allocate the array.
  59. */
  60. #ifndef CONFIG_SPL_BUILD
  61. if (!region)
  62. region = calloc(sizeof(*region), count);
  63. #endif
  64. if (!region)
  65. return NULL;
  66. for (i = 0; i < count; i++) {
  67. debug("%10x %10x\n", fdt_regions[i].offset,
  68. fdt_regions[i].size);
  69. region[i].data = fit + fdt_regions[i].offset;
  70. region[i].size = fdt_regions[i].size;
  71. }
  72. return region;
  73. }
  74. static int fit_image_setup_verify(struct image_sign_info *info,
  75. const void *fit, int noffset, int required_keynode,
  76. char **err_msgp)
  77. {
  78. char *algo_name;
  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. memset(info, '\0', sizeof(*info));
  84. info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
  85. info->fit = (void *)fit;
  86. info->node_offset = noffset;
  87. info->algo = image_get_sig_algo(algo_name);
  88. info->fdt_blob = gd_fdt_blob();
  89. info->required_keynode = required_keynode;
  90. printf("%s:%s", algo_name, info->keyname);
  91. if (!info->algo) {
  92. *err_msgp = "Unknown signature algorithm";
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  98. size_t size, int required_keynode, char **err_msgp)
  99. {
  100. struct image_sign_info info;
  101. struct image_region region;
  102. uint8_t *fit_value;
  103. int fit_value_len;
  104. *err_msgp = NULL;
  105. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  106. err_msgp))
  107. return -1;
  108. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  109. &fit_value_len)) {
  110. *err_msgp = "Can't get hash value property";
  111. return -1;
  112. }
  113. region.data = data;
  114. region.size = size;
  115. if (info.algo->verify(&info, &region, 1, fit_value, fit_value_len)) {
  116. *err_msgp = "Verification failed";
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. static int fit_image_verify_sig(const void *fit, int image_noffset,
  122. const char *data, size_t size, const void *sig_blob,
  123. int sig_offset)
  124. {
  125. int noffset;
  126. char *err_msg = "";
  127. int verified = 0;
  128. int ret;
  129. /* Process all hash subnodes of the component image node */
  130. for (noffset = fdt_first_subnode(fit, image_noffset);
  131. noffset >= 0;
  132. noffset = fdt_next_subnode(fit, noffset)) {
  133. const char *name = fit_get_name(fit, noffset, NULL);
  134. if (!strncmp(name, FIT_SIG_NODENAME,
  135. strlen(FIT_SIG_NODENAME))) {
  136. ret = fit_image_check_sig(fit, noffset, data,
  137. size, -1, &err_msg);
  138. if (ret) {
  139. puts("- ");
  140. } else {
  141. puts("+ ");
  142. verified = 1;
  143. break;
  144. }
  145. }
  146. }
  147. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  148. err_msg = "Corrupted or truncated tree";
  149. goto error;
  150. }
  151. return verified ? 0 : -EPERM;
  152. error:
  153. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  154. err_msg, fit_get_name(fit, noffset, NULL),
  155. fit_get_name(fit, image_noffset, NULL));
  156. return -1;
  157. }
  158. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  159. const char *data, size_t size, const void *sig_blob,
  160. int *no_sigsp)
  161. {
  162. int verify_count = 0;
  163. int noffset;
  164. int sig_node;
  165. /* Work out what we need to verify */
  166. *no_sigsp = 1;
  167. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  168. if (sig_node < 0) {
  169. debug("%s: No signature node found: %s\n", __func__,
  170. fdt_strerror(sig_node));
  171. return 0;
  172. }
  173. for (noffset = fdt_first_subnode(sig_blob, sig_node);
  174. noffset >= 0;
  175. noffset = fdt_next_subnode(sig_blob, noffset)) {
  176. const char *required;
  177. int ret;
  178. required = fdt_getprop(sig_blob, noffset, "required", NULL);
  179. if (!required || strcmp(required, "image"))
  180. continue;
  181. ret = fit_image_verify_sig(fit, image_noffset, data, size,
  182. sig_blob, noffset);
  183. if (ret) {
  184. printf("Failed to verify required signature '%s'\n",
  185. fit_get_name(sig_blob, noffset, NULL));
  186. return ret;
  187. }
  188. verify_count++;
  189. }
  190. if (verify_count)
  191. *no_sigsp = 0;
  192. return 0;
  193. }
  194. int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
  195. char **err_msgp)
  196. {
  197. char * const exc_prop[] = {"data"};
  198. const char *prop, *end, *name;
  199. struct image_sign_info info;
  200. const uint32_t *strings;
  201. uint8_t *fit_value;
  202. int fit_value_len;
  203. int max_regions;
  204. int i, prop_len;
  205. char path[200];
  206. int count;
  207. debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
  208. fit_get_name(fit, noffset, NULL),
  209. fit_get_name(gd_fdt_blob(), required_keynode, NULL));
  210. *err_msgp = NULL;
  211. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  212. err_msgp))
  213. return -1;
  214. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  215. &fit_value_len)) {
  216. *err_msgp = "Can't get hash value property";
  217. return -1;
  218. }
  219. /* Count the number of strings in the property */
  220. prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
  221. end = prop ? prop + prop_len : prop;
  222. for (name = prop, count = 0; name < end; name++)
  223. if (!*name)
  224. count++;
  225. if (!count) {
  226. *err_msgp = "Can't get hashed-nodes property";
  227. return -1;
  228. }
  229. /* Add a sanity check here since we are using the stack */
  230. if (count > IMAGE_MAX_HASHED_NODES) {
  231. *err_msgp = "Number of hashed nodes exceeds maximum";
  232. return -1;
  233. }
  234. /* Create a list of node names from those strings */
  235. char *node_inc[count];
  236. debug("Hash nodes (%d):\n", count);
  237. for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
  238. debug(" '%s'\n", name);
  239. node_inc[i] = (char *)name;
  240. }
  241. /*
  242. * Each node can generate one region for each sub-node. Allow for
  243. * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
  244. */
  245. max_regions = 20 + count * 7;
  246. struct fdt_region fdt_regions[max_regions];
  247. /* Get a list of regions to hash */
  248. count = fdt_find_regions(fit, node_inc, count,
  249. exc_prop, ARRAY_SIZE(exc_prop),
  250. fdt_regions, max_regions - 1,
  251. path, sizeof(path), 0);
  252. if (count < 0) {
  253. *err_msgp = "Failed to hash configuration";
  254. return -1;
  255. }
  256. if (count == 0) {
  257. *err_msgp = "No data to hash";
  258. return -1;
  259. }
  260. if (count >= max_regions - 1) {
  261. *err_msgp = "Too many hash regions";
  262. return -1;
  263. }
  264. /* Add the strings */
  265. strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
  266. if (strings) {
  267. fdt_regions[count].offset = fdt_off_dt_strings(fit) +
  268. fdt32_to_cpu(strings[0]);
  269. fdt_regions[count].size = fdt32_to_cpu(strings[1]);
  270. count++;
  271. }
  272. /* Allocate the region list on the stack */
  273. struct image_region region[count];
  274. fit_region_make_list(fit, fdt_regions, count, region);
  275. if (info.algo->verify(&info, region, count, fit_value,
  276. fit_value_len)) {
  277. *err_msgp = "Verification failed";
  278. return -1;
  279. }
  280. return 0;
  281. }
  282. static int fit_config_verify_sig(const void *fit, int conf_noffset,
  283. const void *sig_blob, int sig_offset)
  284. {
  285. int noffset;
  286. char *err_msg = "";
  287. int verified = 0;
  288. int ret;
  289. /* Process all hash subnodes of the component conf node */
  290. for (noffset = fdt_first_subnode(fit, conf_noffset);
  291. noffset >= 0;
  292. noffset = fdt_next_subnode(fit, noffset)) {
  293. const char *name = fit_get_name(fit, noffset, NULL);
  294. if (!strncmp(name, FIT_SIG_NODENAME,
  295. strlen(FIT_SIG_NODENAME))) {
  296. ret = fit_config_check_sig(fit, noffset, sig_offset,
  297. &err_msg);
  298. if (ret) {
  299. puts("- ");
  300. } else {
  301. puts("+ ");
  302. verified = 1;
  303. break;
  304. }
  305. }
  306. }
  307. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  308. err_msg = "Corrupted or truncated tree";
  309. goto error;
  310. }
  311. return verified ? 0 : -EPERM;
  312. error:
  313. printf(" error!\n%s for '%s' hash node in '%s' config node\n",
  314. err_msg, fit_get_name(fit, noffset, NULL),
  315. fit_get_name(fit, conf_noffset, NULL));
  316. return -1;
  317. }
  318. int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
  319. const void *sig_blob)
  320. {
  321. int noffset;
  322. int sig_node;
  323. /* Work out what we need to verify */
  324. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  325. if (sig_node < 0) {
  326. debug("%s: No signature node found: %s\n", __func__,
  327. fdt_strerror(sig_node));
  328. return 0;
  329. }
  330. for (noffset = fdt_first_subnode(sig_blob, sig_node);
  331. noffset >= 0;
  332. noffset = fdt_next_subnode(sig_blob, noffset)) {
  333. const char *required;
  334. int ret;
  335. required = fdt_getprop(sig_blob, noffset, "required", NULL);
  336. if (!required || strcmp(required, "conf"))
  337. continue;
  338. ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
  339. noffset);
  340. if (ret) {
  341. printf("Failed to verify required signature '%s'\n",
  342. fit_get_name(sig_blob, noffset, NULL));
  343. return ret;
  344. }
  345. }
  346. return 0;
  347. }
  348. int fit_config_verify(const void *fit, int conf_noffset)
  349. {
  350. return !fit_config_verify_required_sigs(fit, conf_noffset,
  351. gd_fdt_blob());
  352. }