image-host.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. *
  5. * (C) Copyright 2008 Semihalf
  6. *
  7. * (C) Copyright 2000-2006
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. */
  10. #include "mkimage.h"
  11. #include <bootm.h>
  12. #include <fdt_region.h>
  13. #include <image.h>
  14. #include <version.h>
  15. /**
  16. * fit_set_hash_value - set hash value in requested has node
  17. * @fit: pointer to the FIT format image header
  18. * @noffset: hash node offset
  19. * @value: hash value to be set
  20. * @value_len: hash value length
  21. *
  22. * fit_set_hash_value() attempts to set hash value in a node at offset
  23. * given and returns operation status to the caller.
  24. *
  25. * returns
  26. * 0, on success
  27. * -1, on failure
  28. */
  29. static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
  30. int value_len)
  31. {
  32. int ret;
  33. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  34. if (ret) {
  35. printf("Can't set hash '%s' property for '%s' node(%s)\n",
  36. FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
  37. fdt_strerror(ret));
  38. return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
  39. }
  40. return 0;
  41. }
  42. /**
  43. * fit_image_process_hash - Process a single subnode of the images/ node
  44. *
  45. * Check each subnode and process accordingly. For hash nodes we generate
  46. * a hash of the supplised data and store it in the node.
  47. *
  48. * @fit: pointer to the FIT format image header
  49. * @image_name: name of image being processes (used to display errors)
  50. * @noffset: subnode offset
  51. * @data: data to process
  52. * @size: size of data in bytes
  53. * @return 0 if ok, -1 on error
  54. */
  55. static int fit_image_process_hash(void *fit, const char *image_name,
  56. int noffset, const void *data, size_t size)
  57. {
  58. uint8_t value[FIT_MAX_HASH_LEN];
  59. const char *node_name;
  60. int value_len;
  61. char *algo;
  62. int ret;
  63. node_name = fit_get_name(fit, noffset, NULL);
  64. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  65. printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
  66. node_name, image_name);
  67. return -ENOENT;
  68. }
  69. if (calculate_hash(data, size, algo, value, &value_len)) {
  70. printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
  71. algo, node_name, image_name);
  72. return -EPROTONOSUPPORT;
  73. }
  74. ret = fit_set_hash_value(fit, noffset, value, value_len);
  75. if (ret) {
  76. printf("Can't set hash value for '%s' hash node in '%s' image node\n",
  77. node_name, image_name);
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. /**
  83. * fit_image_write_sig() - write the signature to a FIT
  84. *
  85. * This writes the signature and signer data to the FIT.
  86. *
  87. * @fit: pointer to the FIT format image header
  88. * @noffset: hash node offset
  89. * @value: signature value to be set
  90. * @value_len: signature value length
  91. * @comment: Text comment to write (NULL for none)
  92. *
  93. * returns
  94. * 0, on success
  95. * -FDT_ERR_..., on failure
  96. */
  97. static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
  98. int value_len, const char *comment, const char *region_prop,
  99. int region_proplen, const char *cmdname)
  100. {
  101. int string_size;
  102. int ret;
  103. /*
  104. * Get the current string size, before we update the FIT and add
  105. * more
  106. */
  107. string_size = fdt_size_dt_strings(fit);
  108. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  109. if (!ret) {
  110. ret = fdt_setprop_string(fit, noffset, "signer-name",
  111. "mkimage");
  112. }
  113. if (!ret) {
  114. ret = fdt_setprop_string(fit, noffset, "signer-version",
  115. PLAIN_VERSION);
  116. }
  117. if (comment && !ret)
  118. ret = fdt_setprop_string(fit, noffset, "comment", comment);
  119. if (!ret) {
  120. time_t timestamp = imagetool_get_source_date(cmdname,
  121. time(NULL));
  122. ret = fit_set_timestamp(fit, noffset, timestamp);
  123. }
  124. if (region_prop && !ret) {
  125. uint32_t strdata[2];
  126. ret = fdt_setprop(fit, noffset, "hashed-nodes",
  127. region_prop, region_proplen);
  128. /* This is a legacy offset, it is unused, and must remain 0. */
  129. strdata[0] = 0;
  130. strdata[1] = cpu_to_fdt32(string_size);
  131. if (!ret) {
  132. ret = fdt_setprop(fit, noffset, "hashed-strings",
  133. strdata, sizeof(strdata));
  134. }
  135. }
  136. return ret;
  137. }
  138. static int fit_image_setup_sig(struct image_sign_info *info,
  139. const char *keydir, void *fit, const char *image_name,
  140. int noffset, const char *require_keys, const char *engine_id)
  141. {
  142. const char *node_name;
  143. char *algo_name;
  144. const char *padding_name;
  145. node_name = fit_get_name(fit, noffset, NULL);
  146. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  147. printf("Can't get algo property for '%s' signature node in '%s' image node\n",
  148. node_name, image_name);
  149. return -1;
  150. }
  151. padding_name = fdt_getprop(fit, noffset, "padding", NULL);
  152. memset(info, '\0', sizeof(*info));
  153. info->keydir = keydir;
  154. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  155. info->fit = fit;
  156. info->node_offset = noffset;
  157. info->name = strdup(algo_name);
  158. info->checksum = image_get_checksum_algo(algo_name);
  159. info->crypto = image_get_crypto_algo(algo_name);
  160. info->padding = image_get_padding_algo(padding_name);
  161. info->require_keys = require_keys;
  162. info->engine_id = engine_id;
  163. if (!info->checksum || !info->crypto) {
  164. printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
  165. algo_name, node_name, image_name);
  166. return -1;
  167. }
  168. return 0;
  169. }
  170. /**
  171. * fit_image_process_sig- Process a single subnode of the images/ node
  172. *
  173. * Check each subnode and process accordingly. For signature nodes we
  174. * generate a signed hash of the supplised data and store it in the node.
  175. *
  176. * @keydir: Directory containing keys to use for signing
  177. * @keydest: Destination FDT blob to write public keys into
  178. * @fit: pointer to the FIT format image header
  179. * @image_name: name of image being processes (used to display errors)
  180. * @noffset: subnode offset
  181. * @data: data to process
  182. * @size: size of data in bytes
  183. * @comment: Comment to add to signature nodes
  184. * @require_keys: Mark all keys as 'required'
  185. * @engine_id: Engine to use for signing
  186. * @return 0 if ok, -1 on error
  187. */
  188. static int fit_image_process_sig(const char *keydir, void *keydest,
  189. void *fit, const char *image_name,
  190. int noffset, const void *data, size_t size,
  191. const char *comment, int require_keys, const char *engine_id,
  192. const char *cmdname)
  193. {
  194. struct image_sign_info info;
  195. struct image_region region;
  196. const char *node_name;
  197. uint8_t *value;
  198. uint value_len;
  199. int ret;
  200. if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
  201. require_keys ? "image" : NULL, engine_id))
  202. return -1;
  203. node_name = fit_get_name(fit, noffset, NULL);
  204. region.data = data;
  205. region.size = size;
  206. ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
  207. if (ret) {
  208. printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
  209. node_name, image_name, ret);
  210. /* We allow keys to be missing */
  211. if (ret == -ENOENT)
  212. return 0;
  213. return -1;
  214. }
  215. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  216. NULL, 0, cmdname);
  217. if (ret) {
  218. if (ret == -FDT_ERR_NOSPACE)
  219. return -ENOSPC;
  220. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  221. node_name, image_name, fdt_strerror(ret));
  222. return -1;
  223. }
  224. free(value);
  225. /* Get keyname again, as FDT has changed and invalidated our pointer */
  226. info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  227. /*
  228. * Write the public key into the supplied FDT file; this might fail
  229. * several times, since we try signing with successively increasing
  230. * size values
  231. */
  232. if (keydest) {
  233. ret = info.crypto->add_verify_data(&info, keydest);
  234. if (ret) {
  235. printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
  236. node_name, image_name);
  237. return ret;
  238. }
  239. }
  240. return 0;
  241. }
  242. static int fit_image_read_data(char *filename, unsigned char *data,
  243. int expected_size)
  244. {
  245. struct stat sbuf;
  246. int fd, ret = -1;
  247. ssize_t n;
  248. /* Open file */
  249. fd = open(filename, O_RDONLY | O_BINARY);
  250. if (fd < 0) {
  251. printf("Can't open file %s (err=%d => %s)\n",
  252. filename, errno, strerror(errno));
  253. return -1;
  254. }
  255. /* Compute file size */
  256. if (fstat(fd, &sbuf) < 0) {
  257. printf("Can't fstat file %s (err=%d => %s)\n",
  258. filename, errno, strerror(errno));
  259. goto err;
  260. }
  261. /* Check file size */
  262. if (sbuf.st_size != expected_size) {
  263. printf("File %s don't have the expected size (size=%ld, expected=%d)\n",
  264. filename, sbuf.st_size, expected_size);
  265. goto err;
  266. }
  267. /* Read data */
  268. n = read(fd, data, sbuf.st_size);
  269. if (n < 0) {
  270. printf("Can't read file %s (err=%d => %s)\n",
  271. filename, errno, strerror(errno));
  272. goto err;
  273. }
  274. /* Check that we have read all the file */
  275. if (n != sbuf.st_size) {
  276. printf("Can't read all file %s (read %zd bytes, expexted %ld)\n",
  277. filename, n, sbuf.st_size);
  278. goto err;
  279. }
  280. ret = 0;
  281. err:
  282. close(fd);
  283. return ret;
  284. }
  285. static int fit_image_setup_cipher(struct image_cipher_info *info,
  286. const char *keydir, void *fit,
  287. const char *image_name, int image_noffset,
  288. const char *node_name, int noffset)
  289. {
  290. char *algo_name;
  291. char filename[128];
  292. int ret = -1;
  293. if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
  294. printf("Can't get algo name for cipher '%s' in image '%s'\n",
  295. node_name, image_name);
  296. goto out;
  297. }
  298. info->keydir = keydir;
  299. /* Read the key name */
  300. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  301. if (!info->keyname) {
  302. printf("Can't get key name for cipher '%s' in image '%s'\n",
  303. node_name, image_name);
  304. goto out;
  305. }
  306. /* Read the IV name */
  307. info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
  308. if (!info->ivname) {
  309. printf("Can't get iv name for cipher '%s' in image '%s'\n",
  310. node_name, image_name);
  311. goto out;
  312. }
  313. info->fit = fit;
  314. info->node_noffset = noffset;
  315. info->name = algo_name;
  316. info->cipher = image_get_cipher_algo(algo_name);
  317. if (!info->cipher) {
  318. printf("Can't get algo for cipher '%s'\n", image_name);
  319. goto out;
  320. }
  321. /* Read the key in the file */
  322. snprintf(filename, sizeof(filename), "%s/%s%s",
  323. info->keydir, info->keyname, ".bin");
  324. info->key = malloc(info->cipher->key_len);
  325. if (!info->key) {
  326. printf("Can't allocate memory for key\n");
  327. ret = -1;
  328. goto out;
  329. }
  330. ret = fit_image_read_data(filename, (unsigned char *)info->key,
  331. info->cipher->key_len);
  332. if (ret < 0)
  333. goto out;
  334. /* Read the IV in the file */
  335. snprintf(filename, sizeof(filename), "%s/%s%s",
  336. info->keydir, info->ivname, ".bin");
  337. info->iv = malloc(info->cipher->iv_len);
  338. if (!info->iv) {
  339. printf("Can't allocate memory for iv\n");
  340. ret = -1;
  341. goto out;
  342. }
  343. ret = fit_image_read_data(filename, (unsigned char *)info->iv,
  344. info->cipher->iv_len);
  345. out:
  346. return ret;
  347. }
  348. int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
  349. const void *data, size_t size,
  350. unsigned char *data_ciphered, int data_ciphered_len)
  351. {
  352. int ret = -1;
  353. /* Remove unciphered data */
  354. ret = fdt_delprop(fit, image_noffset, FIT_DATA_PROP);
  355. if (ret) {
  356. printf("Can't remove data (err = %d)\n", ret);
  357. goto out;
  358. }
  359. /* Add ciphered data */
  360. ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
  361. data_ciphered, data_ciphered_len);
  362. if (ret) {
  363. printf("Can't add ciphered data (err = %d)\n", ret);
  364. goto out;
  365. }
  366. /* add non ciphered data size */
  367. ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
  368. if (ret) {
  369. printf("Can't add unciphered data size (err = %d)\n", ret);
  370. goto out;
  371. }
  372. out:
  373. return ret;
  374. }
  375. static int
  376. fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
  377. const char *image_name, int image_noffset,
  378. const char *node_name, int node_noffset,
  379. const void *data, size_t size,
  380. const char *cmdname)
  381. {
  382. struct image_cipher_info info;
  383. unsigned char *data_ciphered = NULL;
  384. int data_ciphered_len;
  385. int ret;
  386. memset(&info, 0, sizeof(info));
  387. ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
  388. image_noffset, node_name, node_noffset);
  389. if (ret)
  390. goto out;
  391. ret = info.cipher->encrypt(&info, data, size,
  392. &data_ciphered, &data_ciphered_len);
  393. if (ret)
  394. goto out;
  395. /*
  396. * Write the public key into the supplied FDT file; this might fail
  397. * several times, since we try signing with successively increasing
  398. * size values
  399. */
  400. if (keydest) {
  401. ret = info.cipher->add_cipher_data(&info, keydest);
  402. if (ret) {
  403. printf("Failed to add verification data for cipher '%s' in image '%s'\n",
  404. info.keyname, image_name);
  405. goto out;
  406. }
  407. }
  408. ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
  409. data, size,
  410. data_ciphered, data_ciphered_len);
  411. out:
  412. free(data_ciphered);
  413. free((void *)info.key);
  414. free((void *)info.iv);
  415. return ret;
  416. }
  417. int fit_image_cipher_data(const char *keydir, void *keydest,
  418. void *fit, int image_noffset, const char *comment,
  419. int require_keys, const char *engine_id,
  420. const char *cmdname)
  421. {
  422. const char *image_name;
  423. const void *data;
  424. size_t size;
  425. int node_noffset;
  426. /* Get image name */
  427. image_name = fit_get_name(fit, image_noffset, NULL);
  428. if (!image_name) {
  429. printf("Can't get image name\n");
  430. return -1;
  431. }
  432. /* Get image data and data length */
  433. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  434. printf("Can't get image data/size\n");
  435. return -1;
  436. }
  437. /* Process all hash subnodes of the component image node */
  438. for (node_noffset = fdt_first_subnode(fit, image_noffset);
  439. node_noffset >= 0;
  440. node_noffset = fdt_next_subnode(fit, node_noffset)) {
  441. const char *node_name;
  442. int ret = 0;
  443. node_name = fit_get_name(fit, node_noffset, NULL);
  444. if (!node_name) {
  445. printf("Can't get node name\n");
  446. return -1;
  447. }
  448. if (IMAGE_ENABLE_ENCRYPT && keydir &&
  449. !strncmp(node_name, FIT_CIPHER_NODENAME,
  450. strlen(FIT_CIPHER_NODENAME)))
  451. ret = fit_image_process_cipher(keydir, keydest,
  452. fit, image_name,
  453. image_noffset,
  454. node_name, node_noffset,
  455. data, size, cmdname);
  456. if (ret)
  457. return ret;
  458. }
  459. return 0;
  460. }
  461. /**
  462. * fit_image_add_verification_data() - calculate/set verig. data for image node
  463. *
  464. * This adds hash and signature values for an component image node.
  465. *
  466. * All existing hash subnodes are checked, if algorithm property is set to
  467. * one of the supported hash algorithms, hash value is computed and
  468. * corresponding hash node property is set, for example:
  469. *
  470. * Input component image node structure:
  471. *
  472. * o image-1 (at image_noffset)
  473. * | - data = [binary data]
  474. * o hash-1
  475. * |- algo = "sha1"
  476. *
  477. * Output component image node structure:
  478. *
  479. * o image-1 (at image_noffset)
  480. * | - data = [binary data]
  481. * o hash-1
  482. * |- algo = "sha1"
  483. * |- value = sha1(data)
  484. *
  485. * For signature details, please see doc/uImage.FIT/signature.txt
  486. *
  487. * @keydir Directory containing *.key and *.crt files (or NULL)
  488. * @keydest FDT Blob to write public keys into (NULL if none)
  489. * @fit: Pointer to the FIT format image header
  490. * @image_noffset: Requested component image node
  491. * @comment: Comment to add to signature nodes
  492. * @require_keys: Mark all keys as 'required'
  493. * @engine_id: Engine to use for signing
  494. * @return: 0 on success, <0 on failure
  495. */
  496. int fit_image_add_verification_data(const char *keydir, void *keydest,
  497. void *fit, int image_noffset, const char *comment,
  498. int require_keys, const char *engine_id, const char *cmdname)
  499. {
  500. const char *image_name;
  501. const void *data;
  502. size_t size;
  503. int noffset;
  504. /* Get image data and data length */
  505. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  506. printf("Can't get image data/size\n");
  507. return -1;
  508. }
  509. image_name = fit_get_name(fit, image_noffset, NULL);
  510. /* Process all hash subnodes of the component image node */
  511. for (noffset = fdt_first_subnode(fit, image_noffset);
  512. noffset >= 0;
  513. noffset = fdt_next_subnode(fit, noffset)) {
  514. const char *node_name;
  515. int ret = 0;
  516. /*
  517. * Check subnode name, must be equal to "hash" or "signature".
  518. * Multiple hash nodes require unique unit node
  519. * names, e.g. hash-1, hash-2, signature-1, etc.
  520. */
  521. node_name = fit_get_name(fit, noffset, NULL);
  522. if (!strncmp(node_name, FIT_HASH_NODENAME,
  523. strlen(FIT_HASH_NODENAME))) {
  524. ret = fit_image_process_hash(fit, image_name, noffset,
  525. data, size);
  526. } else if (IMAGE_ENABLE_SIGN && keydir &&
  527. !strncmp(node_name, FIT_SIG_NODENAME,
  528. strlen(FIT_SIG_NODENAME))) {
  529. ret = fit_image_process_sig(keydir, keydest,
  530. fit, image_name, noffset, data, size,
  531. comment, require_keys, engine_id, cmdname);
  532. }
  533. if (ret)
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. struct strlist {
  539. int count;
  540. char **strings;
  541. };
  542. static void strlist_init(struct strlist *list)
  543. {
  544. memset(list, '\0', sizeof(*list));
  545. }
  546. static void strlist_free(struct strlist *list)
  547. {
  548. int i;
  549. for (i = 0; i < list->count; i++)
  550. free(list->strings[i]);
  551. free(list->strings);
  552. }
  553. static int strlist_add(struct strlist *list, const char *str)
  554. {
  555. char *dup;
  556. dup = strdup(str);
  557. list->strings = realloc(list->strings,
  558. (list->count + 1) * sizeof(char *));
  559. if (!list || !str)
  560. return -1;
  561. list->strings[list->count++] = dup;
  562. return 0;
  563. }
  564. static const char *fit_config_get_image_list(void *fit, int noffset,
  565. int *lenp, int *allow_missingp)
  566. {
  567. static const char default_list[] = FIT_KERNEL_PROP "\0"
  568. FIT_FDT_PROP;
  569. const char *prop;
  570. /* If there is an "image" property, use that */
  571. prop = fdt_getprop(fit, noffset, "sign-images", lenp);
  572. if (prop) {
  573. *allow_missingp = 0;
  574. return *lenp ? prop : NULL;
  575. }
  576. /* Default image list */
  577. *allow_missingp = 1;
  578. *lenp = sizeof(default_list);
  579. return default_list;
  580. }
  581. static int fit_config_get_hash_list(void *fit, int conf_noffset,
  582. int sig_offset, struct strlist *node_inc)
  583. {
  584. int allow_missing;
  585. const char *prop, *iname, *end;
  586. const char *conf_name, *sig_name;
  587. char name[200], path[200];
  588. int image_count;
  589. int ret, len;
  590. conf_name = fit_get_name(fit, conf_noffset, NULL);
  591. sig_name = fit_get_name(fit, sig_offset, NULL);
  592. /*
  593. * Build a list of nodes we need to hash. We always need the root
  594. * node and the configuration.
  595. */
  596. strlist_init(node_inc);
  597. snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
  598. if (strlist_add(node_inc, "/") ||
  599. strlist_add(node_inc, name))
  600. goto err_mem;
  601. /* Get a list of images that we intend to sign */
  602. prop = fit_config_get_image_list(fit, sig_offset, &len,
  603. &allow_missing);
  604. if (!prop)
  605. return 0;
  606. /* Locate the images */
  607. end = prop + len;
  608. image_count = 0;
  609. for (iname = prop; iname < end; iname += strlen(iname) + 1) {
  610. int noffset;
  611. int image_noffset;
  612. int hash_count;
  613. image_noffset = fit_conf_get_prop_node(fit, conf_noffset,
  614. iname);
  615. if (image_noffset < 0) {
  616. printf("Failed to find image '%s' in configuration '%s/%s'\n",
  617. iname, conf_name, sig_name);
  618. if (allow_missing)
  619. continue;
  620. return -ENOENT;
  621. }
  622. ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
  623. if (ret < 0)
  624. goto err_path;
  625. if (strlist_add(node_inc, path))
  626. goto err_mem;
  627. snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
  628. conf_name);
  629. /* Add all this image's hashes */
  630. hash_count = 0;
  631. for (noffset = fdt_first_subnode(fit, image_noffset);
  632. noffset >= 0;
  633. noffset = fdt_next_subnode(fit, noffset)) {
  634. const char *name = fit_get_name(fit, noffset, NULL);
  635. if (strncmp(name, FIT_HASH_NODENAME,
  636. strlen(FIT_HASH_NODENAME)))
  637. continue;
  638. ret = fdt_get_path(fit, noffset, path, sizeof(path));
  639. if (ret < 0)
  640. goto err_path;
  641. if (strlist_add(node_inc, path))
  642. goto err_mem;
  643. hash_count++;
  644. }
  645. if (!hash_count) {
  646. printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
  647. conf_name, sig_name, iname);
  648. return -ENOMSG;
  649. }
  650. image_count++;
  651. }
  652. if (!image_count) {
  653. printf("Failed to find any images for configuration '%s/%s'\n",
  654. conf_name, sig_name);
  655. return -ENOMSG;
  656. }
  657. return 0;
  658. err_mem:
  659. printf("Out of memory processing configuration '%s/%s'\n", conf_name,
  660. sig_name);
  661. return -ENOMEM;
  662. err_path:
  663. printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
  664. iname, conf_name, sig_name, fdt_strerror(ret));
  665. return -ENOENT;
  666. }
  667. static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
  668. struct image_region **regionp, int *region_countp,
  669. char **region_propp, int *region_proplen)
  670. {
  671. char * const exc_prop[] = {"data"};
  672. struct strlist node_inc;
  673. struct image_region *region;
  674. struct fdt_region fdt_regions[100];
  675. const char *conf_name, *sig_name;
  676. char path[200];
  677. int count, i;
  678. char *region_prop;
  679. int ret, len;
  680. conf_name = fit_get_name(fit, conf_noffset, NULL);
  681. sig_name = fit_get_name(fit, noffset, NULL);
  682. debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
  683. /* Get a list of nodes we want to hash */
  684. ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
  685. if (ret)
  686. return ret;
  687. /* Get a list of regions to hash */
  688. count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
  689. exc_prop, ARRAY_SIZE(exc_prop),
  690. fdt_regions, ARRAY_SIZE(fdt_regions),
  691. path, sizeof(path), 1);
  692. if (count < 0) {
  693. printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
  694. sig_name, fdt_strerror(ret));
  695. return -EIO;
  696. }
  697. if (count == 0) {
  698. printf("No data to hash for configuration '%s/%s': %s\n",
  699. conf_name, sig_name, fdt_strerror(ret));
  700. return -EINVAL;
  701. }
  702. /* Build our list of data blocks */
  703. region = fit_region_make_list(fit, fdt_regions, count, NULL);
  704. if (!region) {
  705. printf("Out of memory hashing configuration '%s/%s'\n",
  706. conf_name, sig_name);
  707. return -ENOMEM;
  708. }
  709. /* Create a list of all hashed properties */
  710. debug("Hash nodes:\n");
  711. for (i = len = 0; i < node_inc.count; i++) {
  712. debug(" %s\n", node_inc.strings[i]);
  713. len += strlen(node_inc.strings[i]) + 1;
  714. }
  715. region_prop = malloc(len);
  716. if (!region_prop) {
  717. printf("Out of memory setting up regions for configuration '%s/%s'\n",
  718. conf_name, sig_name);
  719. return -ENOMEM;
  720. }
  721. for (i = len = 0; i < node_inc.count;
  722. len += strlen(node_inc.strings[i]) + 1, i++)
  723. strcpy(region_prop + len, node_inc.strings[i]);
  724. strlist_free(&node_inc);
  725. *region_countp = count;
  726. *regionp = region;
  727. *region_propp = region_prop;
  728. *region_proplen = len;
  729. return 0;
  730. }
  731. static int fit_config_process_sig(const char *keydir, void *keydest,
  732. void *fit, const char *conf_name, int conf_noffset,
  733. int noffset, const char *comment, int require_keys,
  734. const char *engine_id, const char *cmdname)
  735. {
  736. struct image_sign_info info;
  737. const char *node_name;
  738. struct image_region *region;
  739. char *region_prop;
  740. int region_proplen;
  741. int region_count;
  742. uint8_t *value;
  743. uint value_len;
  744. int ret;
  745. node_name = fit_get_name(fit, noffset, NULL);
  746. if (fit_config_get_data(fit, conf_noffset, noffset, &region,
  747. &region_count, &region_prop, &region_proplen))
  748. return -1;
  749. if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
  750. require_keys ? "conf" : NULL, engine_id))
  751. return -1;
  752. ret = info.crypto->sign(&info, region, region_count, &value,
  753. &value_len);
  754. free(region);
  755. if (ret) {
  756. printf("Failed to sign '%s' signature node in '%s' conf node\n",
  757. node_name, conf_name);
  758. /* We allow keys to be missing */
  759. if (ret == -ENOENT)
  760. return 0;
  761. return -1;
  762. }
  763. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  764. region_prop, region_proplen, cmdname);
  765. if (ret) {
  766. if (ret == -FDT_ERR_NOSPACE)
  767. return -ENOSPC;
  768. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  769. node_name, conf_name, fdt_strerror(ret));
  770. return -1;
  771. }
  772. free(value);
  773. free(region_prop);
  774. /* Get keyname again, as FDT has changed and invalidated our pointer */
  775. info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  776. /* Write the public key into the supplied FDT file */
  777. if (keydest) {
  778. ret = info.crypto->add_verify_data(&info, keydest);
  779. if (ret) {
  780. printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
  781. node_name, conf_name);
  782. }
  783. return ret;
  784. }
  785. return 0;
  786. }
  787. static int fit_config_add_verification_data(const char *keydir, void *keydest,
  788. void *fit, int conf_noffset, const char *comment,
  789. int require_keys, const char *engine_id, const char *cmdname)
  790. {
  791. const char *conf_name;
  792. int noffset;
  793. conf_name = fit_get_name(fit, conf_noffset, NULL);
  794. /* Process all hash subnodes of the configuration node */
  795. for (noffset = fdt_first_subnode(fit, conf_noffset);
  796. noffset >= 0;
  797. noffset = fdt_next_subnode(fit, noffset)) {
  798. const char *node_name;
  799. int ret = 0;
  800. node_name = fit_get_name(fit, noffset, NULL);
  801. if (!strncmp(node_name, FIT_SIG_NODENAME,
  802. strlen(FIT_SIG_NODENAME))) {
  803. ret = fit_config_process_sig(keydir, keydest,
  804. fit, conf_name, conf_noffset, noffset, comment,
  805. require_keys, engine_id, cmdname);
  806. }
  807. if (ret)
  808. return ret;
  809. }
  810. return 0;
  811. }
  812. int fit_cipher_data(const char *keydir, void *keydest, void *fit,
  813. const char *comment, int require_keys,
  814. const char *engine_id, const char *cmdname)
  815. {
  816. int images_noffset;
  817. int noffset;
  818. int ret;
  819. /* Find images parent node offset */
  820. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  821. if (images_noffset < 0) {
  822. printf("Can't find images parent node '%s' (%s)\n",
  823. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  824. return images_noffset;
  825. }
  826. /* Process its subnodes, print out component images details */
  827. for (noffset = fdt_first_subnode(fit, images_noffset);
  828. noffset >= 0;
  829. noffset = fdt_next_subnode(fit, noffset)) {
  830. /*
  831. * Direct child node of the images parent node,
  832. * i.e. component image node.
  833. */
  834. ret = fit_image_cipher_data(keydir, keydest,
  835. fit, noffset, comment,
  836. require_keys, engine_id,
  837. cmdname);
  838. if (ret)
  839. return ret;
  840. }
  841. return 0;
  842. }
  843. int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
  844. const char *comment, int require_keys,
  845. const char *engine_id, const char *cmdname)
  846. {
  847. int images_noffset, confs_noffset;
  848. int noffset;
  849. int ret;
  850. /* Find images parent node offset */
  851. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  852. if (images_noffset < 0) {
  853. printf("Can't find images parent node '%s' (%s)\n",
  854. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  855. return images_noffset;
  856. }
  857. /* Process its subnodes, print out component images details */
  858. for (noffset = fdt_first_subnode(fit, images_noffset);
  859. noffset >= 0;
  860. noffset = fdt_next_subnode(fit, noffset)) {
  861. /*
  862. * Direct child node of the images parent node,
  863. * i.e. component image node.
  864. */
  865. ret = fit_image_add_verification_data(keydir, keydest,
  866. fit, noffset, comment, require_keys, engine_id,
  867. cmdname);
  868. if (ret)
  869. return ret;
  870. }
  871. /* If there are no keys, we can't sign configurations */
  872. if (!IMAGE_ENABLE_SIGN || !keydir)
  873. return 0;
  874. /* Find configurations parent node offset */
  875. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  876. if (confs_noffset < 0) {
  877. printf("Can't find images parent node '%s' (%s)\n",
  878. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  879. return -ENOENT;
  880. }
  881. /* Process its subnodes, print out component images details */
  882. for (noffset = fdt_first_subnode(fit, confs_noffset);
  883. noffset >= 0;
  884. noffset = fdt_next_subnode(fit, noffset)) {
  885. ret = fit_config_add_verification_data(keydir, keydest,
  886. fit, noffset, comment,
  887. require_keys,
  888. engine_id, cmdname);
  889. if (ret)
  890. return ret;
  891. }
  892. return 0;
  893. }
  894. #ifdef CONFIG_FIT_SIGNATURE
  895. int fit_check_sign(const void *fit, const void *key,
  896. const char *fit_uname_config)
  897. {
  898. int cfg_noffset;
  899. int ret;
  900. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  901. if (!cfg_noffset)
  902. return -1;
  903. printf("Verifying Hash Integrity for node '%s'... ",
  904. fdt_get_name(fit, cfg_noffset, NULL));
  905. ret = fit_config_verify(fit, cfg_noffset);
  906. if (ret)
  907. return ret;
  908. printf("Verified OK, loading images\n");
  909. ret = bootm_host_load_images(fit, cfg_noffset);
  910. return ret;
  911. }
  912. #endif