image-host.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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=%lld, expected=%d)\n",
  264. filename, (long long)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 %lld)\n",
  277. filename, n, (long long)sbuf.st_size);
  278. goto err;
  279. }
  280. ret = 0;
  281. err:
  282. close(fd);
  283. return ret;
  284. }
  285. static int get_random_data(void *data, int size)
  286. {
  287. unsigned char *tmp = data;
  288. struct timespec date;
  289. int i, ret = 0;
  290. if (!tmp) {
  291. printf("%s: pointer data is NULL\n", __func__);
  292. ret = -1;
  293. goto out;
  294. }
  295. ret = clock_gettime(CLOCK_MONOTONIC, &date);
  296. if (ret < 0) {
  297. printf("%s: clock_gettime has failed (err=%d, str=%s)\n",
  298. __func__, ret, strerror(errno));
  299. goto out;
  300. }
  301. srandom(date.tv_nsec);
  302. for (i = 0; i < size; i++) {
  303. *tmp = random() & 0xff;
  304. tmp++;
  305. }
  306. out:
  307. return ret;
  308. }
  309. static int fit_image_setup_cipher(struct image_cipher_info *info,
  310. const char *keydir, void *fit,
  311. const char *image_name, int image_noffset,
  312. int noffset)
  313. {
  314. char *algo_name;
  315. char filename[128];
  316. int ret = -1;
  317. if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
  318. printf("Can't get algo name for cipher in image '%s'\n",
  319. image_name);
  320. goto out;
  321. }
  322. info->keydir = keydir;
  323. /* Read the key name */
  324. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  325. if (!info->keyname) {
  326. printf("Can't get key name for cipher in image '%s'\n",
  327. image_name);
  328. goto out;
  329. }
  330. /*
  331. * Read the IV name
  332. *
  333. * If this property is not provided then mkimage will generate
  334. * a random IV and store it in the FIT image
  335. */
  336. info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
  337. info->fit = fit;
  338. info->node_noffset = noffset;
  339. info->name = algo_name;
  340. info->cipher = image_get_cipher_algo(algo_name);
  341. if (!info->cipher) {
  342. printf("Can't get algo for cipher '%s'\n", image_name);
  343. goto out;
  344. }
  345. /* Read the key in the file */
  346. snprintf(filename, sizeof(filename), "%s/%s%s",
  347. info->keydir, info->keyname, ".bin");
  348. info->key = malloc(info->cipher->key_len);
  349. if (!info->key) {
  350. printf("Can't allocate memory for key\n");
  351. ret = -1;
  352. goto out;
  353. }
  354. ret = fit_image_read_data(filename, (unsigned char *)info->key,
  355. info->cipher->key_len);
  356. if (ret < 0)
  357. goto out;
  358. info->iv = malloc(info->cipher->iv_len);
  359. if (!info->iv) {
  360. printf("Can't allocate memory for iv\n");
  361. ret = -1;
  362. goto out;
  363. }
  364. if (info->ivname) {
  365. /* Read the IV in the file */
  366. snprintf(filename, sizeof(filename), "%s/%s%s",
  367. info->keydir, info->ivname, ".bin");
  368. ret = fit_image_read_data(filename, (unsigned char *)info->iv,
  369. info->cipher->iv_len);
  370. } else {
  371. /* Generate an ramdom IV */
  372. ret = get_random_data((void *)info->iv, info->cipher->iv_len);
  373. }
  374. out:
  375. return ret;
  376. }
  377. int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
  378. const void *data, size_t size,
  379. unsigned char *data_ciphered, int data_ciphered_len)
  380. {
  381. int ret = -1;
  382. /* Replace data with ciphered data */
  383. ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
  384. data_ciphered, data_ciphered_len);
  385. if (ret == -FDT_ERR_NOSPACE) {
  386. ret = -ENOSPC;
  387. goto out;
  388. }
  389. if (ret) {
  390. printf("Can't replace data with ciphered data (err = %d)\n", ret);
  391. goto out;
  392. }
  393. /* add non ciphered data size */
  394. ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
  395. if (ret == -FDT_ERR_NOSPACE) {
  396. ret = -ENOSPC;
  397. goto out;
  398. }
  399. if (ret) {
  400. printf("Can't add unciphered data size (err = %d)\n", ret);
  401. goto out;
  402. }
  403. out:
  404. return ret;
  405. }
  406. static int
  407. fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
  408. const char *image_name, int image_noffset,
  409. int node_noffset, const void *data, size_t size,
  410. const char *cmdname)
  411. {
  412. struct image_cipher_info info;
  413. unsigned char *data_ciphered = NULL;
  414. int data_ciphered_len;
  415. int ret;
  416. memset(&info, 0, sizeof(info));
  417. ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
  418. image_noffset, node_noffset);
  419. if (ret)
  420. goto out;
  421. ret = info.cipher->encrypt(&info, data, size,
  422. &data_ciphered, &data_ciphered_len);
  423. if (ret)
  424. goto out;
  425. /*
  426. * Write the public key into the supplied FDT file; this might fail
  427. * several times, since we try signing with successively increasing
  428. * size values
  429. * And, if needed, write the iv in the FIT file
  430. */
  431. if (keydest) {
  432. ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
  433. if (ret) {
  434. printf("Failed to add verification data for cipher '%s' in image '%s'\n",
  435. info.keyname, image_name);
  436. goto out;
  437. }
  438. }
  439. ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
  440. data, size,
  441. data_ciphered, data_ciphered_len);
  442. out:
  443. free(data_ciphered);
  444. free((void *)info.key);
  445. free((void *)info.iv);
  446. return ret;
  447. }
  448. int fit_image_cipher_data(const char *keydir, void *keydest,
  449. void *fit, int image_noffset, const char *comment,
  450. int require_keys, const char *engine_id,
  451. const char *cmdname)
  452. {
  453. const char *image_name;
  454. const void *data;
  455. size_t size;
  456. int cipher_node_offset, len;
  457. /* Get image name */
  458. image_name = fit_get_name(fit, image_noffset, NULL);
  459. if (!image_name) {
  460. printf("Can't get image name\n");
  461. return -1;
  462. }
  463. /* Get image data and data length */
  464. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  465. printf("Can't get image data/size\n");
  466. return -1;
  467. }
  468. /*
  469. * Don't cipher ciphered data.
  470. *
  471. * If the data-size-unciphered property is present the data for this
  472. * image is already encrypted. This is important as 'mkimage -F' can be
  473. * run multiple times on a FIT image.
  474. */
  475. if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len))
  476. return 0;
  477. if (len != -FDT_ERR_NOTFOUND) {
  478. printf("Failure testing for data-size-unciphered\n");
  479. return -1;
  480. }
  481. /* Process cipher node if present */
  482. cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
  483. FIT_CIPHER_NODENAME);
  484. if (cipher_node_offset == -FDT_ERR_NOTFOUND)
  485. return 0;
  486. if (cipher_node_offset < 0) {
  487. printf("Failure getting cipher node\n");
  488. return -1;
  489. }
  490. if (!IMAGE_ENABLE_ENCRYPT || !keydir)
  491. return 0;
  492. return fit_image_process_cipher(keydir, keydest, fit, image_name,
  493. image_noffset, cipher_node_offset, data, size, cmdname);
  494. }
  495. /**
  496. * fit_image_add_verification_data() - calculate/set verig. data for image node
  497. *
  498. * This adds hash and signature values for an component image node.
  499. *
  500. * All existing hash subnodes are checked, if algorithm property is set to
  501. * one of the supported hash algorithms, hash value is computed and
  502. * corresponding hash node property is set, for example:
  503. *
  504. * Input component image node structure:
  505. *
  506. * o image-1 (at image_noffset)
  507. * | - data = [binary data]
  508. * o hash-1
  509. * |- algo = "sha1"
  510. *
  511. * Output component image node structure:
  512. *
  513. * o image-1 (at image_noffset)
  514. * | - data = [binary data]
  515. * o hash-1
  516. * |- algo = "sha1"
  517. * |- value = sha1(data)
  518. *
  519. * For signature details, please see doc/uImage.FIT/signature.txt
  520. *
  521. * @keydir Directory containing *.key and *.crt files (or NULL)
  522. * @keydest FDT Blob to write public keys into (NULL if none)
  523. * @fit: Pointer to the FIT format image header
  524. * @image_noffset: Requested component image node
  525. * @comment: Comment to add to signature nodes
  526. * @require_keys: Mark all keys as 'required'
  527. * @engine_id: Engine to use for signing
  528. * @return: 0 on success, <0 on failure
  529. */
  530. int fit_image_add_verification_data(const char *keydir, void *keydest,
  531. void *fit, int image_noffset, const char *comment,
  532. int require_keys, const char *engine_id, const char *cmdname)
  533. {
  534. const char *image_name;
  535. const void *data;
  536. size_t size;
  537. int noffset;
  538. /* Get image data and data length */
  539. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  540. printf("Can't get image data/size\n");
  541. return -1;
  542. }
  543. image_name = fit_get_name(fit, image_noffset, NULL);
  544. /* Process all hash subnodes of the component image node */
  545. for (noffset = fdt_first_subnode(fit, image_noffset);
  546. noffset >= 0;
  547. noffset = fdt_next_subnode(fit, noffset)) {
  548. const char *node_name;
  549. int ret = 0;
  550. /*
  551. * Check subnode name, must be equal to "hash" or "signature".
  552. * Multiple hash nodes require unique unit node
  553. * names, e.g. hash-1, hash-2, signature-1, etc.
  554. */
  555. node_name = fit_get_name(fit, noffset, NULL);
  556. if (!strncmp(node_name, FIT_HASH_NODENAME,
  557. strlen(FIT_HASH_NODENAME))) {
  558. ret = fit_image_process_hash(fit, image_name, noffset,
  559. data, size);
  560. } else if (IMAGE_ENABLE_SIGN && keydir &&
  561. !strncmp(node_name, FIT_SIG_NODENAME,
  562. strlen(FIT_SIG_NODENAME))) {
  563. ret = fit_image_process_sig(keydir, keydest,
  564. fit, image_name, noffset, data, size,
  565. comment, require_keys, engine_id, cmdname);
  566. }
  567. if (ret)
  568. return ret;
  569. }
  570. return 0;
  571. }
  572. struct strlist {
  573. int count;
  574. char **strings;
  575. };
  576. static void strlist_init(struct strlist *list)
  577. {
  578. memset(list, '\0', sizeof(*list));
  579. }
  580. static void strlist_free(struct strlist *list)
  581. {
  582. int i;
  583. for (i = 0; i < list->count; i++)
  584. free(list->strings[i]);
  585. free(list->strings);
  586. }
  587. static int strlist_add(struct strlist *list, const char *str)
  588. {
  589. char *dup;
  590. dup = strdup(str);
  591. list->strings = realloc(list->strings,
  592. (list->count + 1) * sizeof(char *));
  593. if (!list || !str)
  594. return -1;
  595. list->strings[list->count++] = dup;
  596. return 0;
  597. }
  598. static const char *fit_config_get_image_list(void *fit, int noffset,
  599. int *lenp, int *allow_missingp)
  600. {
  601. static const char default_list[] = FIT_KERNEL_PROP "\0"
  602. FIT_FDT_PROP;
  603. const char *prop;
  604. /* If there is an "image" property, use that */
  605. prop = fdt_getprop(fit, noffset, "sign-images", lenp);
  606. if (prop) {
  607. *allow_missingp = 0;
  608. return *lenp ? prop : NULL;
  609. }
  610. /* Default image list */
  611. *allow_missingp = 1;
  612. *lenp = sizeof(default_list);
  613. return default_list;
  614. }
  615. static int fit_config_add_hash(void *fit, const char *conf_name, const char *sig_name,
  616. struct strlist *node_inc, const char *iname, int image_noffset)
  617. {
  618. char name[200], path[200];
  619. int noffset;
  620. int hash_count;
  621. int ret;
  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. /* Add this image's cipher node if present */
  651. noffset = fdt_subnode_offset(fit, image_noffset,
  652. FIT_CIPHER_NODENAME);
  653. if (noffset != -FDT_ERR_NOTFOUND) {
  654. if (noffset < 0) {
  655. printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n",
  656. conf_name, sig_name, iname,
  657. fdt_strerror(noffset));
  658. return -EIO;
  659. }
  660. ret = fdt_get_path(fit, noffset, path, sizeof(path));
  661. if (ret < 0)
  662. goto err_path;
  663. if (strlist_add(node_inc, path))
  664. goto err_mem;
  665. }
  666. return 0;
  667. err_mem:
  668. printf("Out of memory processing configuration '%s/%s'\n", conf_name,
  669. sig_name);
  670. return -ENOMEM;
  671. err_path:
  672. printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
  673. iname, conf_name, sig_name, fdt_strerror(ret));
  674. return -ENOENT;
  675. }
  676. static int fit_config_get_hash_list(void *fit, int conf_noffset,
  677. int sig_offset, struct strlist *node_inc)
  678. {
  679. int allow_missing;
  680. const char *prop, *iname, *end;
  681. const char *conf_name, *sig_name;
  682. char name[200];
  683. int image_count;
  684. int ret, len;
  685. conf_name = fit_get_name(fit, conf_noffset, NULL);
  686. sig_name = fit_get_name(fit, sig_offset, NULL);
  687. /*
  688. * Build a list of nodes we need to hash. We always need the root
  689. * node and the configuration.
  690. */
  691. strlist_init(node_inc);
  692. snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
  693. if (strlist_add(node_inc, "/") ||
  694. strlist_add(node_inc, name))
  695. goto err_mem;
  696. /* Get a list of images that we intend to sign */
  697. prop = fit_config_get_image_list(fit, sig_offset, &len,
  698. &allow_missing);
  699. if (!prop)
  700. return 0;
  701. /* Locate the images */
  702. end = prop + len;
  703. image_count = 0;
  704. for (iname = prop; iname < end; iname += strlen(iname) + 1) {
  705. int image_noffset;
  706. int index, max_index;
  707. max_index = fdt_stringlist_count(fit, conf_noffset, iname);
  708. for (index = 0; index < max_index; index++) {
  709. image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset,
  710. iname, index);
  711. if (image_noffset < 0) {
  712. printf("Failed to find image '%s' in configuration '%s/%s'\n",
  713. iname, conf_name, sig_name);
  714. if (allow_missing)
  715. continue;
  716. return -ENOENT;
  717. }
  718. ret = fit_config_add_hash(fit, conf_name,
  719. sig_name, node_inc,
  720. iname, image_noffset);
  721. if (ret < 0)
  722. return ret;
  723. image_count++;
  724. }
  725. }
  726. if (!image_count) {
  727. printf("Failed to find any images for configuration '%s/%s'\n",
  728. conf_name, sig_name);
  729. return -ENOMSG;
  730. }
  731. return 0;
  732. err_mem:
  733. printf("Out of memory processing configuration '%s/%s'\n", conf_name,
  734. sig_name);
  735. return -ENOMEM;
  736. }
  737. static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
  738. struct image_region **regionp, int *region_countp,
  739. char **region_propp, int *region_proplen)
  740. {
  741. char * const exc_prop[] = {"data"};
  742. struct strlist node_inc;
  743. struct image_region *region;
  744. struct fdt_region fdt_regions[100];
  745. const char *conf_name, *sig_name;
  746. char path[200];
  747. int count, i;
  748. char *region_prop;
  749. int ret, len;
  750. conf_name = fit_get_name(fit, conf_noffset, NULL);
  751. sig_name = fit_get_name(fit, noffset, NULL);
  752. debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
  753. /* Get a list of nodes we want to hash */
  754. ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
  755. if (ret)
  756. return ret;
  757. /* Get a list of regions to hash */
  758. count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
  759. exc_prop, ARRAY_SIZE(exc_prop),
  760. fdt_regions, ARRAY_SIZE(fdt_regions),
  761. path, sizeof(path), 1);
  762. if (count < 0) {
  763. printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
  764. sig_name, fdt_strerror(ret));
  765. return -EIO;
  766. }
  767. if (count == 0) {
  768. printf("No data to hash for configuration '%s/%s': %s\n",
  769. conf_name, sig_name, fdt_strerror(ret));
  770. return -EINVAL;
  771. }
  772. /* Build our list of data blocks */
  773. region = fit_region_make_list(fit, fdt_regions, count, NULL);
  774. if (!region) {
  775. printf("Out of memory hashing configuration '%s/%s'\n",
  776. conf_name, sig_name);
  777. return -ENOMEM;
  778. }
  779. /* Create a list of all hashed properties */
  780. debug("Hash nodes:\n");
  781. for (i = len = 0; i < node_inc.count; i++) {
  782. debug(" %s\n", node_inc.strings[i]);
  783. len += strlen(node_inc.strings[i]) + 1;
  784. }
  785. region_prop = malloc(len);
  786. if (!region_prop) {
  787. printf("Out of memory setting up regions for configuration '%s/%s'\n",
  788. conf_name, sig_name);
  789. return -ENOMEM;
  790. }
  791. for (i = len = 0; i < node_inc.count;
  792. len += strlen(node_inc.strings[i]) + 1, i++)
  793. strcpy(region_prop + len, node_inc.strings[i]);
  794. strlist_free(&node_inc);
  795. *region_countp = count;
  796. *regionp = region;
  797. *region_propp = region_prop;
  798. *region_proplen = len;
  799. return 0;
  800. }
  801. static int fit_config_process_sig(const char *keydir, void *keydest,
  802. void *fit, const char *conf_name, int conf_noffset,
  803. int noffset, const char *comment, int require_keys,
  804. const char *engine_id, const char *cmdname)
  805. {
  806. struct image_sign_info info;
  807. const char *node_name;
  808. struct image_region *region;
  809. char *region_prop;
  810. int region_proplen;
  811. int region_count;
  812. uint8_t *value;
  813. uint value_len;
  814. int ret;
  815. node_name = fit_get_name(fit, noffset, NULL);
  816. if (fit_config_get_data(fit, conf_noffset, noffset, &region,
  817. &region_count, &region_prop, &region_proplen))
  818. return -1;
  819. if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
  820. require_keys ? "conf" : NULL, engine_id))
  821. return -1;
  822. ret = info.crypto->sign(&info, region, region_count, &value,
  823. &value_len);
  824. free(region);
  825. if (ret) {
  826. printf("Failed to sign '%s' signature node in '%s' conf node\n",
  827. node_name, conf_name);
  828. /* We allow keys to be missing */
  829. if (ret == -ENOENT)
  830. return 0;
  831. return -1;
  832. }
  833. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  834. region_prop, region_proplen, cmdname);
  835. if (ret) {
  836. if (ret == -FDT_ERR_NOSPACE)
  837. return -ENOSPC;
  838. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  839. node_name, conf_name, fdt_strerror(ret));
  840. return -1;
  841. }
  842. free(value);
  843. free(region_prop);
  844. /* Get keyname again, as FDT has changed and invalidated our pointer */
  845. info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  846. /* Write the public key into the supplied FDT file */
  847. if (keydest) {
  848. ret = info.crypto->add_verify_data(&info, keydest);
  849. if (ret) {
  850. printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
  851. node_name, conf_name);
  852. }
  853. return ret;
  854. }
  855. return 0;
  856. }
  857. static int fit_config_add_verification_data(const char *keydir, void *keydest,
  858. void *fit, int conf_noffset, const char *comment,
  859. int require_keys, const char *engine_id, const char *cmdname)
  860. {
  861. const char *conf_name;
  862. int noffset;
  863. conf_name = fit_get_name(fit, conf_noffset, NULL);
  864. /* Process all hash subnodes of the configuration node */
  865. for (noffset = fdt_first_subnode(fit, conf_noffset);
  866. noffset >= 0;
  867. noffset = fdt_next_subnode(fit, noffset)) {
  868. const char *node_name;
  869. int ret = 0;
  870. node_name = fit_get_name(fit, noffset, NULL);
  871. if (!strncmp(node_name, FIT_SIG_NODENAME,
  872. strlen(FIT_SIG_NODENAME))) {
  873. ret = fit_config_process_sig(keydir, keydest,
  874. fit, conf_name, conf_noffset, noffset, comment,
  875. require_keys, engine_id, cmdname);
  876. }
  877. if (ret)
  878. return ret;
  879. }
  880. return 0;
  881. }
  882. int fit_cipher_data(const char *keydir, void *keydest, void *fit,
  883. const char *comment, int require_keys,
  884. const char *engine_id, const char *cmdname)
  885. {
  886. int images_noffset;
  887. int noffset;
  888. int ret;
  889. /* Find images parent node offset */
  890. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  891. if (images_noffset < 0) {
  892. printf("Can't find images parent node '%s' (%s)\n",
  893. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  894. return images_noffset;
  895. }
  896. /* Process its subnodes, print out component images details */
  897. for (noffset = fdt_first_subnode(fit, images_noffset);
  898. noffset >= 0;
  899. noffset = fdt_next_subnode(fit, noffset)) {
  900. /*
  901. * Direct child node of the images parent node,
  902. * i.e. component image node.
  903. */
  904. ret = fit_image_cipher_data(keydir, keydest,
  905. fit, noffset, comment,
  906. require_keys, engine_id,
  907. cmdname);
  908. if (ret)
  909. return ret;
  910. }
  911. return 0;
  912. }
  913. int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
  914. const char *comment, int require_keys,
  915. const char *engine_id, const char *cmdname)
  916. {
  917. int images_noffset, confs_noffset;
  918. int noffset;
  919. int ret;
  920. /* Find images parent node offset */
  921. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  922. if (images_noffset < 0) {
  923. printf("Can't find images parent node '%s' (%s)\n",
  924. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  925. return images_noffset;
  926. }
  927. /* Process its subnodes, print out component images details */
  928. for (noffset = fdt_first_subnode(fit, images_noffset);
  929. noffset >= 0;
  930. noffset = fdt_next_subnode(fit, noffset)) {
  931. /*
  932. * Direct child node of the images parent node,
  933. * i.e. component image node.
  934. */
  935. ret = fit_image_add_verification_data(keydir, keydest,
  936. fit, noffset, comment, require_keys, engine_id,
  937. cmdname);
  938. if (ret)
  939. return ret;
  940. }
  941. /* If there are no keys, we can't sign configurations */
  942. if (!IMAGE_ENABLE_SIGN || !keydir)
  943. return 0;
  944. /* Find configurations parent node offset */
  945. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  946. if (confs_noffset < 0) {
  947. printf("Can't find images parent node '%s' (%s)\n",
  948. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  949. return -ENOENT;
  950. }
  951. /* Process its subnodes, print out component images details */
  952. for (noffset = fdt_first_subnode(fit, confs_noffset);
  953. noffset >= 0;
  954. noffset = fdt_next_subnode(fit, noffset)) {
  955. ret = fit_config_add_verification_data(keydir, keydest,
  956. fit, noffset, comment,
  957. require_keys,
  958. engine_id, cmdname);
  959. if (ret)
  960. return ret;
  961. }
  962. return 0;
  963. }
  964. #ifdef CONFIG_FIT_SIGNATURE
  965. int fit_check_sign(const void *fit, const void *key,
  966. const char *fit_uname_config)
  967. {
  968. int cfg_noffset;
  969. int ret;
  970. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  971. if (!cfg_noffset)
  972. return -1;
  973. printf("Verifying Hash Integrity for node '%s'... ",
  974. fdt_get_name(fit, cfg_noffset, NULL));
  975. ret = fit_config_verify(fit, cfg_noffset);
  976. if (ret)
  977. return ret;
  978. printf("Verified OK, loading images\n");
  979. ret = bootm_host_load_images(fit, cfg_noffset);
  980. return ret;
  981. }
  982. #endif