fconf_cot_getter.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright (c) 2020-2024, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stddef.h>
  8. #include <mbedtls/version.h>
  9. #include <common/fdt_wrappers.h>
  10. #include <common/tbbr/cot_def.h>
  11. #include <drivers/auth/auth_mod.h>
  12. #include <lib/fconf/fconf.h>
  13. #include <lib/object_pool.h>
  14. #include <libfdt.h>
  15. #include <tools_share/tbbr_oid.h>
  16. /* static structures used during authentication process */
  17. static auth_param_type_desc_t sig = AUTH_PARAM_TYPE_DESC(
  18. AUTH_PARAM_SIG, 0);
  19. static auth_param_type_desc_t sig_alg = AUTH_PARAM_TYPE_DESC(
  20. AUTH_PARAM_SIG_ALG, 0);
  21. static auth_param_type_desc_t raw_data = AUTH_PARAM_TYPE_DESC(
  22. AUTH_PARAM_RAW_DATA, 0);
  23. /* pointers to an array of CoT descriptors */
  24. static const auth_img_desc_t *cot_desc[MAX_NUMBER_IDS];
  25. /* array of CoT descriptors */
  26. static auth_img_desc_t auth_img_descs[MAX_NUMBER_IDS];
  27. /* array of authentication methods structures */
  28. static auth_method_desc_t auth_methods[MAX_NUMBER_IDS * AUTH_METHOD_NUM];
  29. static OBJECT_POOL_ARRAY(auth_methods_pool, auth_methods);
  30. /* array of authentication params structures */
  31. static auth_param_desc_t auth_params[MAX_NUMBER_IDS * COT_MAX_VERIFIED_PARAMS];
  32. static OBJECT_POOL_ARRAY(auth_params_pool, auth_params);
  33. /* array of authentication param type structures */
  34. static auth_param_type_desc_t auth_param_type_descs[MAX_NUMBER_IDS];
  35. static OBJECT_POOL_ARRAY(auth_param_type_descs_pool, auth_param_type_descs);
  36. /*
  37. * array of OIDs
  38. * Object IDs are used to search hash, pk, counter values in certificate.
  39. * As per binding we have below 2 combinations:
  40. * 1. Certificates are validated using nv-cntr and pk
  41. * 2. Raw images are authenticated using hash
  42. * Hence in worst case, there are maximum 2 OIDs per image/certificate
  43. */
  44. static unsigned char oids[(MAX_NUMBER_IDS * 2)][MAX_OID_NAME_LEN];
  45. static OBJECT_POOL_ARRAY(oid_pool, oids);
  46. /* An array of auth buffer which holds hashes and pk
  47. * ToDo: Size decided with the current number of images and
  48. * certificates which are available in CoT. Size of these buffers bound to
  49. * increase in the future on the addition of images/certificates.
  50. */
  51. static unsigned char hash_auth_bufs[20][HASH_DER_LEN];
  52. static OBJECT_POOL_ARRAY(hash_auth_buf_pool, hash_auth_bufs);
  53. static unsigned char pk_auth_bufs[12][PK_DER_LEN];
  54. static OBJECT_POOL_ARRAY(pk_auth_buf_pool, pk_auth_bufs);
  55. /*******************************************************************************
  56. * update_parent_auth_data() - Update authentication data structure
  57. * @auth_desc[in]: Pointer to the auth image descriptor
  58. * @type_desc[in]: Pointer to authentication parameter
  59. * @auth_buf_size[in]: Buffer size to hold pk or hash
  60. *
  61. * Return 0 on success or an error value otherwise.
  62. ******************************************************************************/
  63. static int update_parent_auth_data(const auth_img_desc_t *auth_desc,
  64. auth_param_type_desc_t *type_desc,
  65. unsigned int auth_buf_size)
  66. {
  67. unsigned int i;
  68. auth_param_desc_t *auth_data = &auth_desc->authenticated_data[0];
  69. unsigned char *auth_buf;
  70. for (i = 0U; i < COT_MAX_VERIFIED_PARAMS; i++) {
  71. if (auth_data[i].type_desc == type_desc) {
  72. return 0;
  73. }
  74. if (auth_data[i].type_desc == NULL) {
  75. break;
  76. }
  77. }
  78. if (auth_buf_size == HASH_DER_LEN) {
  79. auth_buf = pool_alloc(&hash_auth_buf_pool);
  80. } else if (auth_buf_size == PK_DER_LEN) {
  81. auth_buf = pool_alloc(&pk_auth_buf_pool);
  82. } else {
  83. return -1;
  84. }
  85. if (i < COT_MAX_VERIFIED_PARAMS) {
  86. auth_data[i].type_desc = type_desc;
  87. auth_data[i].data.ptr = auth_buf;
  88. auth_data[i].data.len = auth_buf_size;
  89. } else {
  90. ERROR("Out of authentication data array\n");
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. /*******************************************************************************
  96. * get_auth_param_type_desc() - Get pointer of authentication parameter
  97. * @img_id[in]: Image Id
  98. * @type_desc[out]: Pointer to authentication parameter
  99. * @buf_size[out]: Buffer size which hold hash/pk
  100. *
  101. * Return 0 on success or an error value otherwise.
  102. ******************************************************************************/
  103. static int get_auth_param_type_desc(unsigned int img_id,
  104. auth_param_type_desc_t **type_desc,
  105. unsigned int *buf_size)
  106. {
  107. auth_method_desc_t *img_auth_method = NULL;
  108. img_type_t type = auth_img_descs[img_id].img_type;
  109. if (type == IMG_CERT) {
  110. img_auth_method =
  111. &auth_img_descs[img_id].img_auth_methods[AUTH_METHOD_SIG];
  112. *type_desc = img_auth_method->param.sig.pk;
  113. *buf_size = PK_DER_LEN;
  114. } else if (type == IMG_RAW) {
  115. img_auth_method =
  116. &auth_img_descs[img_id].img_auth_methods[AUTH_METHOD_HASH];
  117. *type_desc = img_auth_method->param.hash.hash;
  118. *buf_size = HASH_DER_LEN;
  119. } else {
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. /*******************************************************************************
  125. * set_auth_method() - Update global auth image descriptors with authentication
  126. * method data
  127. * @auth_method_type[in]: Type of authentication method
  128. * @oid[in]: Object Idetifier for pk/hash search
  129. * @auth_method[in]: Pointer to authentication method to set
  130. ******************************************************************************/
  131. static void set_auth_method(auth_method_type_t auth_method_type, char *oid,
  132. auth_method_desc_t *auth_method)
  133. {
  134. auth_param_type_t auth_param_type = AUTH_PARAM_NONE;
  135. auth_param_type_desc_t *auth_param_type_desc;
  136. assert(auth_method != NULL);
  137. auth_param_type_desc = pool_alloc(&auth_param_type_descs_pool);
  138. auth_method->type = auth_method_type;
  139. if (auth_method_type == AUTH_METHOD_SIG) {
  140. auth_param_type = AUTH_PARAM_PUB_KEY;
  141. auth_method->param.sig.sig = &sig;
  142. auth_method->param.sig.alg = &sig_alg;
  143. auth_method->param.sig.data = &raw_data;
  144. auth_method->param.sig.pk = auth_param_type_desc;
  145. } else if (auth_method_type == AUTH_METHOD_HASH) {
  146. auth_param_type = AUTH_PARAM_HASH;
  147. auth_method->param.hash.data = &raw_data;
  148. auth_method->param.hash.hash = auth_param_type_desc;
  149. } else if (auth_method_type == AUTH_METHOD_NV_CTR) {
  150. auth_param_type = AUTH_PARAM_NV_CTR;
  151. auth_method->param.nv_ctr.cert_nv_ctr = auth_param_type_desc;
  152. auth_method->param.nv_ctr.plat_nv_ctr = auth_param_type_desc;
  153. }
  154. auth_param_type_desc->type = auth_param_type;
  155. auth_param_type_desc->cookie = (void *)oid;
  156. }
  157. /*******************************************************************************
  158. * get_oid() - get object identifier from device tree
  159. * @dtb[in]: Pointer to the device tree blob in memory
  160. * @node[in]: Offset of the node
  161. * @prop[in]: Property to read from the given node
  162. * @oid[out]: Object Indentifier of key/hash/nv-counter in certificate
  163. *
  164. * Return 0 on success or an error value otherwise.
  165. ******************************************************************************/
  166. static int get_oid(const void *dtb, int node, const char *prop, char **oid)
  167. {
  168. uint32_t phandle;
  169. int rc;
  170. rc = fdt_read_uint32(dtb, node, prop, &phandle);
  171. if (rc < 0) {
  172. return rc;
  173. }
  174. node = fdt_node_offset_by_phandle(dtb, phandle);
  175. if (node < 0) {
  176. return node;
  177. }
  178. *oid = pool_alloc(&oid_pool);
  179. rc = fdtw_read_string(dtb, node, "oid", *oid, MAX_OID_NAME_LEN);
  180. return rc;
  181. }
  182. /*******************************************************************************
  183. * populate_and_set_auth_methods() - Populate auth method parameters from
  184. * device tree and set authentication method
  185. * structure.
  186. * @dtb[in]: Pointer to the device tree blob in memory
  187. * @node[in]: Offset of the node
  188. * @img_id[in]: Image identifier
  189. * @type[in]: Type of image
  190. * @root_certificate[in]:Root certificate (authenticated by ROTPK)
  191. *
  192. * Return 0 on success or an error value otherwise.
  193. ******************************************************************************/
  194. static int populate_and_set_auth_methods(const void *dtb, int node,
  195. unsigned int img_id, img_type_t type,
  196. bool root_certificate)
  197. {
  198. auth_method_type_t auth_method_type = AUTH_METHOD_NONE;
  199. int rc;
  200. char *oid = NULL;
  201. auth_method_desc_t *auth_method = pool_alloc_n(&auth_methods_pool,
  202. AUTH_METHOD_NUM);
  203. /*
  204. * This is as per binding document where certificates are
  205. * verified by signature and images are verified by hash.
  206. */
  207. if (type == IMG_CERT) {
  208. rc = get_oid(dtb, node, "signing-key", &oid);
  209. if (rc < 0) {
  210. /*
  211. * The signing-key property is optional in root
  212. * certificates, mandatory otherwise.
  213. */
  214. if (root_certificate) {
  215. oid = NULL;
  216. } else {
  217. ERROR("FCONF: Can't read %s property\n",
  218. "signing-key");
  219. return rc;
  220. }
  221. }
  222. auth_method_type = AUTH_METHOD_SIG;
  223. } else if (type == IMG_RAW) {
  224. rc = get_oid(dtb, node, "hash", &oid);
  225. if (rc < 0) {
  226. ERROR("FCONF: Can't read %s property\n",
  227. "hash");
  228. return rc;
  229. }
  230. auth_method_type = AUTH_METHOD_HASH;
  231. } else {
  232. return -1;
  233. }
  234. set_auth_method(auth_method_type, oid,
  235. &auth_method[auth_method_type]);
  236. /* Retrieve the optional property */
  237. rc = get_oid(dtb, node, "antirollback-counter", &oid);
  238. if (rc == 0) {
  239. auth_method_type = AUTH_METHOD_NV_CTR;
  240. set_auth_method(auth_method_type, oid,
  241. &auth_method[auth_method_type]);
  242. }
  243. auth_img_descs[img_id].img_auth_methods = &auth_method[0];
  244. return 0;
  245. }
  246. /*******************************************************************************
  247. * get_parent_img_id() - Get parent image id for given child node
  248. * @dtb[in]: Pointer to the device tree blob in memory
  249. * @node[in]: Offset of the child node
  250. * @parent_img_id[out]: Image id of parent
  251. *
  252. * Return 0 on success or an error value otherwise.
  253. ******************************************************************************/
  254. static int get_parent_img_id(const void *dtb, int node,
  255. unsigned int *parent_img_id)
  256. {
  257. uint32_t phandle;
  258. int err;
  259. err = fdt_read_uint32(dtb, node, "parent", &phandle);
  260. if (err < 0) {
  261. ERROR("FCONF: Could not read %s property in node\n",
  262. "parent");
  263. return err;
  264. }
  265. node = fdt_node_offset_by_phandle(dtb, phandle);
  266. if (node < 0) {
  267. ERROR("FCONF: Failed to locate node using its phandle\n");
  268. return node;
  269. }
  270. err = fdt_read_uint32(dtb, node, "image-id", parent_img_id);
  271. if (err < 0) {
  272. ERROR("FCONF: Could not read %s property in node\n",
  273. "image-id");
  274. }
  275. return err;
  276. }
  277. /*******************************************************************************
  278. * set_desc_data() - Update data in descriptor's structure
  279. * @dtb[in]: Pointer to the device tree blob in memory
  280. * @node[in]: Offset of the node
  281. * @type[in]: Type of image (RAW/CERT)
  282. *
  283. * Return 0 on success or an error value otherwise.
  284. ******************************************************************************/
  285. static int set_desc_data(const void *dtb, int node, img_type_t type)
  286. {
  287. int rc;
  288. bool root_certificate = false;
  289. unsigned int img_id, parent_img_id;
  290. rc = fdt_read_uint32(dtb, node, "image-id", &img_id);
  291. if (rc < 0) {
  292. ERROR("FCONF: Can't find property %s in node\n",
  293. "image-id");
  294. return rc;
  295. }
  296. if (fdt_getprop(dtb, node, "root-certificate",
  297. NULL) != NULL) {
  298. root_certificate = true;
  299. }
  300. if (!root_certificate) {
  301. rc = get_parent_img_id(dtb, node, &parent_img_id);
  302. if (rc < 0) {
  303. return rc;
  304. }
  305. auth_img_descs[img_id].parent = &auth_img_descs[parent_img_id];
  306. }
  307. auth_img_descs[img_id].img_id = img_id;
  308. auth_img_descs[img_id].img_type = type;
  309. rc = populate_and_set_auth_methods(dtb, node, img_id, type,
  310. root_certificate);
  311. if (rc < 0) {
  312. return rc;
  313. }
  314. if (type == IMG_CERT) {
  315. auth_param_desc_t *auth_param =
  316. pool_alloc_n(&auth_params_pool,
  317. COT_MAX_VERIFIED_PARAMS);
  318. auth_img_descs[img_id].authenticated_data = &auth_param[0];
  319. }
  320. cot_desc[img_id] = &auth_img_descs[img_id];
  321. return rc;
  322. }
  323. /*******************************************************************************
  324. * populate_manifest_descs() - Populate CoT descriptors and update global
  325. * certificate structures
  326. * @dtb[in]: Pointer to the device tree blob in memory
  327. *
  328. * Return 0 on success or an error value otherwise.
  329. ******************************************************************************/
  330. static int populate_manifest_descs(const void *dtb)
  331. {
  332. int node, child;
  333. int rc;
  334. /*
  335. * Assert the node offset points to "arm, cert-descs"
  336. * compatible property
  337. */
  338. const char *compatible_str = "arm, cert-descs";
  339. node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
  340. if (node < 0) {
  341. ERROR("FCONF: Can't find %s compatible in node\n",
  342. compatible_str);
  343. return node;
  344. }
  345. fdt_for_each_subnode(child, dtb, node) {
  346. rc = set_desc_data(dtb, child, IMG_CERT);
  347. if (rc < 0) {
  348. return rc;
  349. }
  350. }
  351. return 0;
  352. }
  353. /*******************************************************************************
  354. * populate_image_descs() - Populate CoT descriptors and update global
  355. * image descriptor structures.
  356. * @dtb[in]: Pointer to the device tree blob in memory
  357. *
  358. * Return 0 on success or an error value otherwise.
  359. ******************************************************************************/
  360. static int populate_image_descs(const void *dtb)
  361. {
  362. int node, child;
  363. int rc;
  364. /*
  365. * Assert the node offset points to "arm, img-descs"
  366. * compatible property
  367. */
  368. const char *compatible_str = "arm, img-descs";
  369. node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
  370. if (node < 0) {
  371. ERROR("FCONF: Can't find %s compatible in node\n",
  372. compatible_str);
  373. return node;
  374. }
  375. fdt_for_each_subnode(child, dtb, node) {
  376. rc = set_desc_data(dtb, child, IMG_RAW);
  377. if (rc < 0) {
  378. return rc;
  379. }
  380. }
  381. return 0;
  382. }
  383. /*******************************************************************************
  384. * fconf_populate_cot_descs() - Populate CoT descriptors and update global
  385. * structures
  386. * @config[in]: Pointer to the device tree blob in memory
  387. *
  388. * Return 0 on success or an error value otherwise.
  389. ******************************************************************************/
  390. static int fconf_populate_cot_descs(uintptr_t config)
  391. {
  392. auth_param_type_desc_t *type_desc = NULL;
  393. unsigned int auth_buf_size = 0U;
  394. int rc;
  395. /* As libfdt uses void *, we can't avoid this cast */
  396. const void *dtb = (void *)config;
  397. /* populate manifest descs information */
  398. rc = populate_manifest_descs(dtb);
  399. if (rc < 0) {
  400. ERROR("FCONF: population of %s descs failed %d\n",
  401. "manifest", rc);
  402. return rc;
  403. }
  404. /* populate image descs information */
  405. rc = populate_image_descs(dtb);
  406. if (rc < 0) {
  407. ERROR("FCONF: population of %s descs failed %d\n",
  408. "images", rc);
  409. return rc;
  410. }
  411. /* update parent's authentication data */
  412. for (unsigned int i = 0U; i < MAX_NUMBER_IDS; i++) {
  413. if (auth_img_descs[i].parent != NULL) {
  414. rc = get_auth_param_type_desc(i,
  415. &type_desc,
  416. &auth_buf_size);
  417. if (rc < 0) {
  418. ERROR("FCONF: failed to get auth data %d\n",
  419. rc);
  420. return rc;
  421. }
  422. rc = update_parent_auth_data(auth_img_descs[i].parent,
  423. type_desc,
  424. auth_buf_size);
  425. if (rc < 0) {
  426. ERROR("FCONF: auth data update failed %d\n",
  427. rc);
  428. return rc;
  429. }
  430. }
  431. }
  432. return rc;
  433. }
  434. FCONF_REGISTER_POPULATOR(TB_FW, cot_desc, fconf_populate_cot_descs);
  435. REGISTER_COT(cot_desc);