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