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