image-sig.c 10 KB

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