auth_mod.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <platform_def.h>
  10. #include <common/debug.h>
  11. #include <common/tbbr/cot_def.h>
  12. #include <drivers/auth/auth_common.h>
  13. #include <drivers/auth/auth_mod.h>
  14. #include <drivers/auth/crypto_mod.h>
  15. #include <drivers/auth/img_parser_mod.h>
  16. #include <drivers/fwu/fwu.h>
  17. #include <lib/fconf/fconf_tbbr_getter.h>
  18. #include <plat/common/platform.h>
  19. #include <tools_share/zero_oid.h>
  20. /* ASN.1 tags */
  21. #define ASN1_INTEGER 0x02
  22. #pragma weak plat_set_nv_ctr2
  23. static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
  24. const auth_param_type_desc_t *b)
  25. {
  26. if ((a->type == b->type) && (a->cookie == b->cookie)) {
  27. return 0;
  28. }
  29. return 1;
  30. }
  31. /*
  32. * This function obtains the requested authentication parameter data from the
  33. * information extracted from the parent image after its authentication.
  34. */
  35. static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
  36. const auth_img_desc_t *img_desc,
  37. void **param, unsigned int *len)
  38. {
  39. int i;
  40. if (img_desc->authenticated_data == NULL)
  41. return 1;
  42. for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
  43. if (0 == cmp_auth_param_type_desc(param_type_desc,
  44. img_desc->authenticated_data[i].type_desc)) {
  45. *param = img_desc->authenticated_data[i].data.ptr;
  46. *len = img_desc->authenticated_data[i].data.len;
  47. return 0;
  48. }
  49. }
  50. return 1;
  51. }
  52. /*
  53. * Authenticate an image by matching the data hash
  54. *
  55. * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
  56. * this method, the image must contain:
  57. *
  58. * - The data to calculate the hash from
  59. *
  60. * The parent image must contain:
  61. *
  62. * - The hash to be matched with (including hash algorithm)
  63. *
  64. * For a successful authentication, both hashes must match. The function calls
  65. * the crypto-module to check this matching.
  66. *
  67. * Parameters:
  68. * param: parameters to perform the hash authentication
  69. * img_desc: pointer to image descriptor so we can know the image type
  70. * and parent image
  71. * img: pointer to image in memory
  72. * img_len: length of image (in bytes)
  73. *
  74. * Return:
  75. * 0 = success, Otherwise = error
  76. */
  77. static int auth_hash(const auth_method_param_hash_t *param,
  78. const auth_img_desc_t *img_desc,
  79. void *img, unsigned int img_len)
  80. {
  81. void *data_ptr, *hash_der_ptr;
  82. unsigned int data_len, hash_der_len;
  83. int rc;
  84. /* Get the hash from the parent image. This hash will be DER encoded
  85. * and contain the hash algorithm */
  86. rc = auth_get_param(param->hash, img_desc->parent,
  87. &hash_der_ptr, &hash_der_len);
  88. if (rc != 0) {
  89. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  90. __func__, __LINE__, rc);
  91. return rc;
  92. }
  93. /* Get the data to be hashed from the current image */
  94. rc = img_parser_get_auth_param(img_desc->img_type, param->data,
  95. img, img_len, &data_ptr, &data_len);
  96. if (rc != 0) {
  97. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  98. __func__, __LINE__, rc);
  99. return rc;
  100. }
  101. /* Ask the crypto module to verify this hash */
  102. rc = crypto_mod_verify_hash(data_ptr, data_len,
  103. hash_der_ptr, hash_der_len);
  104. if (rc != 0) {
  105. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  106. __func__, __LINE__, rc);
  107. return rc;
  108. }
  109. return 0;
  110. }
  111. /*
  112. * Authenticate by digital signature
  113. *
  114. * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
  115. * this method, the image must contain:
  116. *
  117. * - Data to be signed
  118. * - Signature
  119. * - Signature algorithm
  120. *
  121. * We rely on the image parser module to extract this data from the image.
  122. * The parent image must contain:
  123. *
  124. * - Public key (or a hash of it)
  125. *
  126. * If the parent image contains only a hash of the key, we will try to obtain
  127. * the public key from the image itself (i.e. self-signed certificates). In that
  128. * case, the signature verification is considered just an integrity check and
  129. * the authentication is established by calculating the hash of the key and
  130. * comparing it with the hash obtained from the parent.
  131. *
  132. * If the image has no parent (NULL), it means it has to be authenticated using
  133. * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
  134. * or a hash of it.
  135. *
  136. * Return: 0 = success, Otherwise = error
  137. */
  138. static int auth_signature(const auth_method_param_sig_t *param,
  139. const auth_img_desc_t *img_desc,
  140. void *img, unsigned int img_len)
  141. {
  142. void *data_ptr, *pk_ptr, *cnv_pk_ptr, *pk_plat_ptr, *sig_ptr, *sig_alg_ptr, *pk_oid;
  143. unsigned int data_len, pk_len, cnv_pk_len, pk_plat_len, sig_len, sig_alg_len;
  144. unsigned int flags = 0;
  145. int rc;
  146. /* Get the data to be signed from current image */
  147. rc = img_parser_get_auth_param(img_desc->img_type, param->data,
  148. img, img_len, &data_ptr, &data_len);
  149. if (rc != 0) {
  150. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  151. __func__, __LINE__, rc);
  152. return rc;
  153. }
  154. /* Get the signature from current image */
  155. rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
  156. img, img_len, &sig_ptr, &sig_len);
  157. if (rc != 0) {
  158. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  159. __func__, __LINE__, rc);
  160. return rc;
  161. }
  162. /* Get the signature algorithm from current image */
  163. rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
  164. img, img_len, &sig_alg_ptr, &sig_alg_len);
  165. if (rc != 0) {
  166. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  167. __func__, __LINE__, rc);
  168. return rc;
  169. }
  170. /* Get the public key from the parent. If there is no parent (NULL),
  171. * the certificate has been signed with the ROTPK, so we have to get
  172. * the PK from the platform */
  173. if (img_desc->parent != NULL) {
  174. rc = auth_get_param(param->pk, img_desc->parent,
  175. &pk_ptr, &pk_len);
  176. if (rc != 0) {
  177. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  178. __func__, __LINE__, rc);
  179. return rc;
  180. }
  181. } else {
  182. /*
  183. * Root certificates are signed with the ROTPK, so we have to
  184. * get it from the platform.
  185. */
  186. rc = plat_get_rotpk_info(param->pk->cookie, &pk_plat_ptr,
  187. &pk_plat_len, &flags);
  188. if (rc != 0) {
  189. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  190. __func__, __LINE__, rc);
  191. return rc;
  192. }
  193. assert(is_rotpk_flags_valid(flags));
  194. /* Also retrieve the key from the image. */
  195. rc = img_parser_get_auth_param(img_desc->img_type,
  196. param->pk, img, img_len,
  197. &pk_ptr, &pk_len);
  198. if (rc != 0) {
  199. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  200. __func__, __LINE__, rc);
  201. return rc;
  202. }
  203. /*
  204. * Validate the certificate's key against the platform ROTPK.
  205. *
  206. * Platform may store key in one of the following way -
  207. * 1. Hash of ROTPK
  208. * 2. Hash if prefixed, suffixed or modified ROTPK
  209. * 3. Full ROTPK
  210. */
  211. if ((flags & ROTPK_NOT_DEPLOYED) != 0U) {
  212. NOTICE("ROTPK is not deployed on platform. "
  213. "Skipping ROTPK verification.\n");
  214. } else if ((flags & ROTPK_IS_HASH) != 0U) {
  215. /*
  216. * platform may store the hash of a prefixed,
  217. * suffixed or modified pk
  218. */
  219. rc = crypto_mod_convert_pk(pk_ptr, pk_len, &cnv_pk_ptr, &cnv_pk_len);
  220. if (rc != 0) {
  221. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  222. __func__, __LINE__, rc);
  223. return rc;
  224. }
  225. /*
  226. * The hash of the certificate's public key must match
  227. * the hash of the ROTPK.
  228. */
  229. rc = crypto_mod_verify_hash(cnv_pk_ptr, cnv_pk_len,
  230. pk_plat_ptr, pk_plat_len);
  231. if (rc != 0) {
  232. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  233. __func__, __LINE__, rc);
  234. return rc;
  235. }
  236. } else {
  237. /* Platform supports full ROTPK */
  238. if ((pk_len != pk_plat_len) ||
  239. (memcmp(pk_plat_ptr, pk_ptr, pk_len) != 0)) {
  240. ERROR("plat and cert ROTPK len mismatch\n");
  241. return -1;
  242. }
  243. }
  244. /*
  245. * Set Zero-OID for ROTPK(subject key) as a the certificate
  246. * does not hold Key-OID information for ROTPK.
  247. */
  248. if (param->pk->cookie != NULL) {
  249. pk_oid = param->pk->cookie;
  250. } else {
  251. pk_oid = ZERO_OID;
  252. }
  253. /*
  254. * Public key is verified at this stage, notify platform
  255. * to measure and publish it.
  256. */
  257. rc = plat_mboot_measure_key(pk_oid, pk_ptr, pk_len);
  258. if (rc != 0) {
  259. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  260. __func__, __LINE__, rc);
  261. }
  262. }
  263. /* Ask the crypto module to verify the signature */
  264. rc = crypto_mod_verify_signature(data_ptr, data_len,
  265. sig_ptr, sig_len,
  266. sig_alg_ptr, sig_alg_len,
  267. pk_ptr, pk_len);
  268. if (rc != 0) {
  269. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  270. __func__, __LINE__, rc);
  271. return rc;
  272. }
  273. return 0;
  274. }
  275. /*
  276. * Authenticate by Non-Volatile counter
  277. *
  278. * To protect the system against rollback, the platform includes a non-volatile
  279. * counter whose value can only be increased. All certificates include a counter
  280. * value that should not be lower than the value stored in the platform. If the
  281. * value is larger, the counter in the platform must be updated to the new value
  282. * (provided it has been authenticated).
  283. *
  284. * Return: 0 = success, Otherwise = error
  285. * Returns additionally,
  286. * cert_nv_ctr -> NV counter value present in the certificate
  287. * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed
  288. * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed
  289. */
  290. static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
  291. const auth_img_desc_t *img_desc,
  292. void *img, unsigned int img_len,
  293. unsigned int *cert_nv_ctr,
  294. bool *need_nv_ctr_upgrade)
  295. {
  296. unsigned char *p;
  297. void *data_ptr = NULL;
  298. unsigned int data_len, len, i;
  299. unsigned int plat_nv_ctr;
  300. int rc;
  301. /* Get the counter value from current image. The AM expects the IPM
  302. * to return the counter value as a DER encoded integer */
  303. rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
  304. img, img_len, &data_ptr, &data_len);
  305. if (rc != 0) {
  306. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  307. __func__, __LINE__, rc);
  308. return rc;
  309. }
  310. /* Parse the DER encoded integer */
  311. assert(data_ptr);
  312. p = (unsigned char *)data_ptr;
  313. /*
  314. * Integers must be at least 3 bytes: 1 for tag, 1 for length, and 1
  315. * for value. The first byte (tag) must be ASN1_INTEGER.
  316. */
  317. if ((data_len < 3) || (*p != ASN1_INTEGER)) {
  318. /* Invalid ASN.1 integer */
  319. return 1;
  320. }
  321. p++;
  322. /*
  323. * NV-counters are unsigned integers up to 31 bits. Trailing
  324. * padding is not allowed.
  325. */
  326. len = (unsigned int)*p;
  327. if ((len > 4) || (data_len - 2 != len)) {
  328. return 1;
  329. }
  330. p++;
  331. /* Check the number is not negative */
  332. if (*p & 0x80) {
  333. return 1;
  334. }
  335. /* Convert to unsigned int. This code is for a little-endian CPU */
  336. *cert_nv_ctr = 0;
  337. for (i = 0; i < len; i++) {
  338. *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
  339. }
  340. /* Get the counter from the platform */
  341. rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
  342. if (rc != 0) {
  343. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  344. __func__, __LINE__, rc);
  345. return rc;
  346. }
  347. if (*cert_nv_ctr < plat_nv_ctr) {
  348. /* Invalid NV-counter */
  349. return 1;
  350. } else if (*cert_nv_ctr > plat_nv_ctr) {
  351. #if PSA_FWU_SUPPORT && IMAGE_BL2
  352. if (fwu_get_active_bank_state() == FWU_BANK_STATE_ACCEPTED) {
  353. *need_nv_ctr_upgrade = true;
  354. } else {
  355. *need_nv_ctr_upgrade = false;
  356. }
  357. #else
  358. *need_nv_ctr_upgrade = true;
  359. #endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */
  360. }
  361. return 0;
  362. }
  363. int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
  364. unsigned int nv_ctr)
  365. {
  366. return plat_set_nv_ctr(cookie, nv_ctr);
  367. }
  368. /*
  369. * Return the parent id in the output parameter '*parent_id'
  370. *
  371. * Return value:
  372. * 0 = Image has parent, 1 = Image has no parent or parent is authenticated
  373. */
  374. int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
  375. {
  376. const auth_img_desc_t *img_desc = NULL;
  377. assert(parent_id != NULL);
  378. /* Get the image descriptor */
  379. img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
  380. /* Check if the image has no parent (ROT) */
  381. if (img_desc->parent == NULL) {
  382. *parent_id = 0;
  383. return 1;
  384. }
  385. /* Check if the parent has already been authenticated */
  386. if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
  387. *parent_id = 0;
  388. return 1;
  389. }
  390. *parent_id = img_desc->parent->img_id;
  391. return 0;
  392. }
  393. /*
  394. * Initialize the different modules in the authentication framework
  395. */
  396. void auth_mod_init(void)
  397. {
  398. /* Check we have a valid CoT registered */
  399. assert(cot_desc_ptr != NULL);
  400. /* Image parser module */
  401. img_parser_init();
  402. }
  403. /*
  404. * Authenticate a certificate/image
  405. *
  406. * Return: 0 = success, Otherwise = error
  407. */
  408. int auth_mod_verify_img(unsigned int img_id,
  409. void *img_ptr,
  410. unsigned int img_len)
  411. {
  412. const auth_img_desc_t *img_desc = NULL;
  413. const auth_param_type_desc_t *type_desc = NULL;
  414. const auth_method_desc_t *auth_method = NULL;
  415. void *param_ptr;
  416. unsigned int param_len;
  417. int rc, i;
  418. unsigned int cert_nv_ctr = 0;
  419. bool need_nv_ctr_upgrade = false;
  420. bool sig_auth_done = false;
  421. const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
  422. /* Get the image descriptor from the chain of trust */
  423. img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
  424. /* Ask the parser to check the image integrity */
  425. rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
  426. if (rc != 0) {
  427. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  428. __func__, __LINE__, rc);
  429. return rc;
  430. }
  431. /* Authenticate the image using the methods indicated in the image
  432. * descriptor. */
  433. if (img_desc->img_auth_methods == NULL)
  434. return 1;
  435. for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
  436. auth_method = &img_desc->img_auth_methods[i];
  437. switch (auth_method->type) {
  438. case AUTH_METHOD_NONE:
  439. rc = 0;
  440. break;
  441. case AUTH_METHOD_HASH:
  442. rc = auth_hash(&auth_method->param.hash,
  443. img_desc, img_ptr, img_len);
  444. break;
  445. case AUTH_METHOD_SIG:
  446. rc = auth_signature(&auth_method->param.sig,
  447. img_desc, img_ptr, img_len);
  448. sig_auth_done = true;
  449. break;
  450. case AUTH_METHOD_NV_CTR:
  451. nv_ctr_param = &auth_method->param.nv_ctr;
  452. rc = auth_nvctr(nv_ctr_param,
  453. img_desc, img_ptr, img_len,
  454. &cert_nv_ctr, &need_nv_ctr_upgrade);
  455. break;
  456. default:
  457. /* Unknown authentication method */
  458. rc = 1;
  459. break;
  460. }
  461. if (rc != 0) {
  462. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  463. __func__, __LINE__, rc);
  464. return rc;
  465. }
  466. }
  467. /*
  468. * Do platform NV counter upgrade only if the certificate gets
  469. * authenticated, and platform NV-counter upgrade is needed.
  470. */
  471. if (need_nv_ctr_upgrade && sig_auth_done) {
  472. rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
  473. img_desc, cert_nv_ctr);
  474. if (rc != 0) {
  475. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  476. __func__, __LINE__, rc);
  477. return rc;
  478. }
  479. }
  480. /* Extract the parameters indicated in the image descriptor to
  481. * authenticate the children images. */
  482. if (img_desc->authenticated_data != NULL) {
  483. for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
  484. if (img_desc->authenticated_data[i].type_desc == NULL) {
  485. continue;
  486. }
  487. /* Get the parameter from the image parser module */
  488. rc = img_parser_get_auth_param(img_desc->img_type,
  489. img_desc->authenticated_data[i].type_desc,
  490. img_ptr, img_len, &param_ptr, &param_len);
  491. if (rc != 0) {
  492. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  493. __func__, __LINE__, rc);
  494. return rc;
  495. }
  496. /* Check parameter size */
  497. if (param_len > img_desc->authenticated_data[i].data.len) {
  498. return 1;
  499. }
  500. /* Copy the parameter for later use */
  501. memcpy((void *)img_desc->authenticated_data[i].data.ptr,
  502. (void *)param_ptr, param_len);
  503. /*
  504. * If this is a public key then measure and publicise
  505. * it.
  506. */
  507. type_desc = img_desc->authenticated_data[i].type_desc;
  508. if (type_desc->type == AUTH_PARAM_PUB_KEY) {
  509. rc = plat_mboot_measure_key(type_desc->cookie,
  510. param_ptr,
  511. param_len);
  512. if (rc != 0) {
  513. VERBOSE("[TBB] %s():%d failed with error code %d.\n",
  514. __func__, __LINE__, rc);
  515. }
  516. }
  517. }
  518. }
  519. /* Mark image as authenticated */
  520. auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
  521. return 0;
  522. }