auth_mod.c 14 KB

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