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