123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- #include <assert.h>
- #include <stdint.h>
- #include <string.h>
- #include <platform_def.h>
- #include <common/debug.h>
- #include <common/tbbr/cot_def.h>
- #include <drivers/auth/auth_common.h>
- #include <drivers/auth/auth_mod.h>
- #include <drivers/auth/crypto_mod.h>
- #include <drivers/auth/img_parser_mod.h>
- #include <drivers/fwu/fwu.h>
- #include <lib/fconf/fconf_tbbr_getter.h>
- #include <plat/common/platform.h>
- #include <tools_share/zero_oid.h>
- #define ASN1_INTEGER 0x02
- #pragma weak plat_set_nv_ctr2
- static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
- const auth_param_type_desc_t *b)
- {
- if ((a->type == b->type) && (a->cookie == b->cookie)) {
- return 0;
- }
- return 1;
- }
- static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
- const auth_img_desc_t *img_desc,
- void **param, unsigned int *len)
- {
- int i;
- if (img_desc->authenticated_data == NULL)
- return 1;
- for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
- if (0 == cmp_auth_param_type_desc(param_type_desc,
- img_desc->authenticated_data[i].type_desc)) {
- *param = img_desc->authenticated_data[i].data.ptr;
- *len = img_desc->authenticated_data[i].data.len;
- return 0;
- }
- }
- return 1;
- }
- static int auth_hash(const auth_method_param_hash_t *param,
- const auth_img_desc_t *img_desc,
- void *img, unsigned int img_len)
- {
- void *data_ptr, *hash_der_ptr;
- unsigned int data_len, hash_der_len;
- int rc;
-
- rc = auth_get_param(param->hash, img_desc->parent,
- &hash_der_ptr, &hash_der_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- rc = img_parser_get_auth_param(img_desc->img_type, param->data,
- img, img_len, &data_ptr, &data_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- rc = crypto_mod_verify_hash(data_ptr, data_len,
- hash_der_ptr, hash_der_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
- return 0;
- }
- static int auth_signature(const auth_method_param_sig_t *param,
- const auth_img_desc_t *img_desc,
- void *img, unsigned int img_len)
- {
- void *data_ptr, *pk_ptr, *cnv_pk_ptr, *pk_plat_ptr, *sig_ptr, *sig_alg_ptr, *pk_oid;
- unsigned int data_len, pk_len, cnv_pk_len, pk_plat_len, sig_len, sig_alg_len;
- unsigned int flags = 0;
- int rc;
-
- rc = img_parser_get_auth_param(img_desc->img_type, param->data,
- img, img_len, &data_ptr, &data_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
- img, img_len, &sig_ptr, &sig_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
- img, img_len, &sig_alg_ptr, &sig_alg_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- if (img_desc->parent != NULL) {
- rc = auth_get_param(param->pk, img_desc->parent,
- &pk_ptr, &pk_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
- } else {
-
- rc = plat_get_rotpk_info(param->pk->cookie, &pk_plat_ptr,
- &pk_plat_len, &flags);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
- assert(is_rotpk_flags_valid(flags));
-
- rc = img_parser_get_auth_param(img_desc->img_type,
- param->pk, img, img_len,
- &pk_ptr, &pk_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- if ((flags & ROTPK_NOT_DEPLOYED) != 0U) {
- NOTICE("ROTPK is not deployed on platform. "
- "Skipping ROTPK verification.\n");
- } else if ((flags & ROTPK_IS_HASH) != 0U) {
-
- rc = crypto_mod_convert_pk(pk_ptr, pk_len, &cnv_pk_ptr, &cnv_pk_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- rc = crypto_mod_verify_hash(cnv_pk_ptr, cnv_pk_len,
- pk_plat_ptr, pk_plat_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
- } else {
-
- if ((pk_len != pk_plat_len) ||
- (memcmp(pk_plat_ptr, pk_ptr, pk_len) != 0)) {
- ERROR("plat and cert ROTPK len mismatch\n");
- return -1;
- }
- }
-
- if (param->pk->cookie != NULL) {
- pk_oid = param->pk->cookie;
- } else {
- pk_oid = ZERO_OID;
- }
-
- rc = plat_mboot_measure_key(pk_oid, pk_ptr, pk_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- }
- }
-
- rc = crypto_mod_verify_signature(data_ptr, data_len,
- sig_ptr, sig_len,
- sig_alg_ptr, sig_alg_len,
- pk_ptr, pk_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
- return 0;
- }
- static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
- const auth_img_desc_t *img_desc,
- void *img, unsigned int img_len,
- unsigned int *cert_nv_ctr,
- bool *need_nv_ctr_upgrade)
- {
- unsigned char *p;
- void *data_ptr = NULL;
- unsigned int data_len, len, i;
- unsigned int plat_nv_ctr;
- int rc;
-
- rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
- img, img_len, &data_ptr, &data_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- assert(data_ptr);
- p = (unsigned char *)data_ptr;
-
- if ((data_len < 3) || (*p != ASN1_INTEGER)) {
-
- return 1;
- }
- p++;
-
- len = (unsigned int)*p;
- if ((len > 4) || (data_len - 2 != len)) {
- return 1;
- }
- p++;
-
- if (*p & 0x80) {
- return 1;
- }
-
- *cert_nv_ctr = 0;
- for (i = 0; i < len; i++) {
- *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
- }
-
- rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
- if (*cert_nv_ctr < plat_nv_ctr) {
-
- return 1;
- } else if (*cert_nv_ctr > plat_nv_ctr) {
- #if PSA_FWU_SUPPORT && IMAGE_BL2
- if (fwu_get_active_bank_state() == FWU_BANK_STATE_ACCEPTED) {
- *need_nv_ctr_upgrade = true;
- } else {
- *need_nv_ctr_upgrade = false;
- }
- #else
- *need_nv_ctr_upgrade = true;
- #endif
- }
- return 0;
- }
- int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
- unsigned int nv_ctr)
- {
- return plat_set_nv_ctr(cookie, nv_ctr);
- }
- int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
- {
- const auth_img_desc_t *img_desc = NULL;
- assert(parent_id != NULL);
-
- img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
-
- if (img_desc->parent == NULL) {
- *parent_id = 0;
- return 1;
- }
-
- if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
- *parent_id = 0;
- return 1;
- }
- *parent_id = img_desc->parent->img_id;
- return 0;
- }
- void auth_mod_init(void)
- {
-
- assert(cot_desc_ptr != NULL);
-
- img_parser_init();
- }
- int auth_mod_verify_img(unsigned int img_id,
- void *img_ptr,
- unsigned int img_len)
- {
- const auth_img_desc_t *img_desc = NULL;
- const auth_param_type_desc_t *type_desc = NULL;
- const auth_method_desc_t *auth_method = NULL;
- void *param_ptr;
- unsigned int param_len;
- int rc, i;
- unsigned int cert_nv_ctr = 0;
- bool need_nv_ctr_upgrade = false;
- bool sig_auth_done = false;
- const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
-
- img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
-
- rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- if (img_desc->img_auth_methods == NULL)
- return 1;
- for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
- auth_method = &img_desc->img_auth_methods[i];
- switch (auth_method->type) {
- case AUTH_METHOD_NONE:
- rc = 0;
- break;
- case AUTH_METHOD_HASH:
- rc = auth_hash(&auth_method->param.hash,
- img_desc, img_ptr, img_len);
- break;
- case AUTH_METHOD_SIG:
- rc = auth_signature(&auth_method->param.sig,
- img_desc, img_ptr, img_len);
- sig_auth_done = true;
- break;
- case AUTH_METHOD_NV_CTR:
- nv_ctr_param = &auth_method->param.nv_ctr;
- rc = auth_nvctr(nv_ctr_param,
- img_desc, img_ptr, img_len,
- &cert_nv_ctr, &need_nv_ctr_upgrade);
- break;
- default:
-
- rc = 1;
- break;
- }
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
- }
-
- if (need_nv_ctr_upgrade && sig_auth_done) {
- rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
- img_desc, cert_nv_ctr);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
- }
-
- if (img_desc->authenticated_data != NULL) {
- for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
- if (img_desc->authenticated_data[i].type_desc == NULL) {
- continue;
- }
-
- rc = img_parser_get_auth_param(img_desc->img_type,
- img_desc->authenticated_data[i].type_desc,
- img_ptr, img_len, ¶m_ptr, ¶m_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- return rc;
- }
-
- if (param_len > img_desc->authenticated_data[i].data.len) {
- return 1;
- }
-
- memcpy((void *)img_desc->authenticated_data[i].data.ptr,
- (void *)param_ptr, param_len);
-
- type_desc = img_desc->authenticated_data[i].type_desc;
- if (type_desc->type == AUTH_PARAM_PUB_KEY) {
- rc = plat_mboot_measure_key(type_desc->cookie,
- param_ptr,
- param_len);
- if (rc != 0) {
- VERBOSE("[TBB] %s():%d failed with error code %d.\n",
- __func__, __LINE__, rc);
- }
- }
- }
- }
-
- auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
- return 0;
- }
|