fconf_cot_getter.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (c) 2020-2023, 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. if (root_certificate) {
  209. oid = NULL;
  210. } else {
  211. rc = get_oid(dtb, node, "signing-key", &oid);
  212. if (rc < 0) {
  213. ERROR("FCONF: Can't read %s property\n",
  214. "signing-key");
  215. return rc;
  216. }
  217. }
  218. auth_method_type = AUTH_METHOD_SIG;
  219. } else if (type == IMG_RAW) {
  220. rc = get_oid(dtb, node, "hash", &oid);
  221. if (rc < 0) {
  222. ERROR("FCONF: Can't read %s property\n",
  223. "hash");
  224. return rc;
  225. }
  226. auth_method_type = AUTH_METHOD_HASH;
  227. } else {
  228. return -1;
  229. }
  230. set_auth_method(auth_method_type, oid,
  231. &auth_method[auth_method_type]);
  232. /* Retrieve the optional property */
  233. rc = get_oid(dtb, node, "antirollback-counter", &oid);
  234. if (rc == 0) {
  235. auth_method_type = AUTH_METHOD_NV_CTR;
  236. set_auth_method(auth_method_type, oid,
  237. &auth_method[auth_method_type]);
  238. }
  239. auth_img_descs[img_id].img_auth_methods = &auth_method[0];
  240. return 0;
  241. }
  242. /*******************************************************************************
  243. * get_parent_img_id() - Get parent image id for given child node
  244. * @dtb[in]: Pointer to the device tree blob in memory
  245. * @node[in]: Offset of the child node
  246. * @parent_img_id[out]: Image id of parent
  247. *
  248. * Return 0 on success or an error value otherwise.
  249. ******************************************************************************/
  250. static int get_parent_img_id(const void *dtb, int node,
  251. unsigned int *parent_img_id)
  252. {
  253. uint32_t phandle;
  254. int err;
  255. err = fdt_read_uint32(dtb, node, "parent", &phandle);
  256. if (err < 0) {
  257. ERROR("FCONF: Could not read %s property in node\n",
  258. "parent");
  259. return err;
  260. }
  261. node = fdt_node_offset_by_phandle(dtb, phandle);
  262. if (node < 0) {
  263. ERROR("FCONF: Failed to locate node using its phandle\n");
  264. return node;
  265. }
  266. err = fdt_read_uint32(dtb, node, "image-id", parent_img_id);
  267. if (err < 0) {
  268. ERROR("FCONF: Could not read %s property in node\n",
  269. "image-id");
  270. }
  271. return err;
  272. }
  273. /*******************************************************************************
  274. * set_desc_data() - Update data in descriptor's structure
  275. * @dtb[in]: Pointer to the device tree blob in memory
  276. * @node[in]: Offset of the node
  277. * @type[in]: Type of image (RAW/CERT)
  278. *
  279. * Return 0 on success or an error value otherwise.
  280. ******************************************************************************/
  281. static int set_desc_data(const void *dtb, int node, img_type_t type)
  282. {
  283. int rc;
  284. bool root_certificate = false;
  285. unsigned int img_id, parent_img_id;
  286. rc = fdt_read_uint32(dtb, node, "image-id", &img_id);
  287. if (rc < 0) {
  288. ERROR("FCONF: Can't find property %s in node\n",
  289. "image-id");
  290. return rc;
  291. }
  292. if (fdt_getprop(dtb, node, "root-certificate",
  293. NULL) != NULL) {
  294. root_certificate = true;
  295. }
  296. if (!root_certificate) {
  297. rc = get_parent_img_id(dtb, node, &parent_img_id);
  298. if (rc < 0) {
  299. return rc;
  300. }
  301. auth_img_descs[img_id].parent = &auth_img_descs[parent_img_id];
  302. }
  303. auth_img_descs[img_id].img_id = img_id;
  304. auth_img_descs[img_id].img_type = type;
  305. rc = populate_and_set_auth_methods(dtb, node, img_id, type,
  306. root_certificate);
  307. if (rc < 0) {
  308. return rc;
  309. }
  310. if (type == IMG_CERT) {
  311. auth_param_desc_t *auth_param =
  312. pool_alloc_n(&auth_params_pool,
  313. COT_MAX_VERIFIED_PARAMS);
  314. auth_img_descs[img_id].authenticated_data = &auth_param[0];
  315. }
  316. cot_desc[img_id] = &auth_img_descs[img_id];
  317. return rc;
  318. }
  319. /*******************************************************************************
  320. * populate_manifest_descs() - Populate CoT descriptors and update global
  321. * certificate structures
  322. * @dtb[in]: Pointer to the device tree blob in memory
  323. *
  324. * Return 0 on success or an error value otherwise.
  325. ******************************************************************************/
  326. static int populate_manifest_descs(const void *dtb)
  327. {
  328. int node, child;
  329. int rc;
  330. /*
  331. * Assert the node offset points to "arm, cert-descs"
  332. * compatible property
  333. */
  334. const char *compatible_str = "arm, cert-descs";
  335. node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
  336. if (node < 0) {
  337. ERROR("FCONF: Can't find %s compatible in node\n",
  338. compatible_str);
  339. return node;
  340. }
  341. fdt_for_each_subnode(child, dtb, node) {
  342. rc = set_desc_data(dtb, child, IMG_CERT);
  343. if (rc < 0) {
  344. return rc;
  345. }
  346. }
  347. return 0;
  348. }
  349. /*******************************************************************************
  350. * populate_image_descs() - Populate CoT descriptors and update global
  351. * image descriptor structures.
  352. * @dtb[in]: Pointer to the device tree blob in memory
  353. *
  354. * Return 0 on success or an error value otherwise.
  355. ******************************************************************************/
  356. static int populate_image_descs(const void *dtb)
  357. {
  358. int node, child;
  359. int rc;
  360. /*
  361. * Assert the node offset points to "arm, img-descs"
  362. * compatible property
  363. */
  364. const char *compatible_str = "arm, img-descs";
  365. node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
  366. if (node < 0) {
  367. ERROR("FCONF: Can't find %s compatible in node\n",
  368. compatible_str);
  369. return node;
  370. }
  371. fdt_for_each_subnode(child, dtb, node) {
  372. rc = set_desc_data(dtb, child, IMG_RAW);
  373. if (rc < 0) {
  374. return rc;
  375. }
  376. }
  377. return 0;
  378. }
  379. /*******************************************************************************
  380. * fconf_populate_cot_descs() - Populate CoT descriptors and update global
  381. * structures
  382. * @config[in]: Pointer to the device tree blob in memory
  383. *
  384. * Return 0 on success or an error value otherwise.
  385. ******************************************************************************/
  386. static int fconf_populate_cot_descs(uintptr_t config)
  387. {
  388. auth_param_type_desc_t *type_desc = NULL;
  389. unsigned int auth_buf_size = 0U;
  390. int rc;
  391. /* As libfdt uses void *, we can't avoid this cast */
  392. const void *dtb = (void *)config;
  393. /* populate manifest descs information */
  394. rc = populate_manifest_descs(dtb);
  395. if (rc < 0) {
  396. ERROR("FCONF: population of %s descs failed %d\n",
  397. "manifest", rc);
  398. return rc;
  399. }
  400. /* populate image descs information */
  401. rc = populate_image_descs(dtb);
  402. if (rc < 0) {
  403. ERROR("FCONF: population of %s descs failed %d\n",
  404. "images", rc);
  405. return rc;
  406. }
  407. /* update parent's authentication data */
  408. for (unsigned int i = 0U; i < MAX_NUMBER_IDS; i++) {
  409. if (auth_img_descs[i].parent != NULL) {
  410. rc = get_auth_param_type_desc(i,
  411. &type_desc,
  412. &auth_buf_size);
  413. if (rc < 0) {
  414. ERROR("FCONF: failed to get auth data %d\n",
  415. rc);
  416. return rc;
  417. }
  418. rc = update_parent_auth_data(auth_img_descs[i].parent,
  419. type_desc,
  420. auth_buf_size);
  421. if (rc < 0) {
  422. ERROR("FCONF: auth data update failed %d\n",
  423. rc);
  424. return rc;
  425. }
  426. }
  427. }
  428. return rc;
  429. }
  430. FCONF_REGISTER_POPULATOR(TB_FW, cot_desc, fconf_populate_cot_descs);
  431. REGISTER_COT(cot_desc);