Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 1 | /* |
Lauren Wehrmeister | 351f2f2 | 2025-04-28 15:41:23 -0500 | [diff] [blame] | 2 | * Copyright (c) 2015-2025, 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> |
Lauren Wehrmeister | 351f2f2 | 2025-04-28 15:41:23 -0500 | [diff] [blame] | 17 | #include <drivers/auth/auth_util.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 18 | #include <drivers/auth/crypto_mod.h> |
| 19 | #include <drivers/auth/img_parser_mod.h> |
Manish V Badarkhe | 04005b7 | 2021-06-20 22:29:22 +0100 | [diff] [blame] | 20 | #include <drivers/fwu/fwu.h> |
Louis Mayencourt | 944ade8 | 2019-08-08 12:03:26 +0100 | [diff] [blame] | 21 | #include <lib/fconf/fconf_tbbr_getter.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 22 | #include <plat/common/platform.h> |
| 23 | |
Manish V Badarkhe | caf03fd | 2023-04-11 12:55:07 +0100 | [diff] [blame] | 24 | #include <tools_share/zero_oid.h> |
| 25 | |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 26 | /* ASN.1 tags */ |
| 27 | #define ASN1_INTEGER 0x02 |
| 28 | |
dp-arm | b3e8580 | 2016-12-12 14:48:13 +0000 | [diff] [blame] | 29 | #pragma weak plat_set_nv_ctr2 |
| 30 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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 | |
Joel Hutton | 69931af | 2019-03-11 11:37:38 +0000 | [diff] [blame] | 50 | if (img_desc->authenticated_data == NULL) |
| 51 | return 1; |
| 52 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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; |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 96 | int rc; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 102 | if (rc != 0) { |
| 103 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 104 | __func__, __LINE__, rc); |
| 105 | return rc; |
| 106 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 111 | if (rc != 0) { |
| 112 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 113 | __func__, __LINE__, rc); |
| 114 | return rc; |
| 115 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 120 | if (rc != 0) { |
| 121 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 122 | __func__, __LINE__, rc); |
| 123 | return rc; |
| 124 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 125 | |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 126 | return 0; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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 | { |
Robin van der Gracht | 5cea313 | 2023-09-13 10:02:38 +0200 | [diff] [blame] | 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; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 162 | unsigned int flags = 0; |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 163 | int rc; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 168 | if (rc != 0) { |
| 169 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 170 | __func__, __LINE__, rc); |
| 171 | return rc; |
| 172 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 177 | if (rc != 0) { |
| 178 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 179 | __func__, __LINE__, rc); |
| 180 | return rc; |
| 181 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 186 | if (rc != 0) { |
| 187 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 188 | __func__, __LINE__, rc); |
| 189 | return rc; |
| 190 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 191 | |
Lauren Wehrmeister | 351f2f2 | 2025-04-28 15:41:23 -0500 | [diff] [blame] | 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 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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 */ |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 207 | if (img_desc->parent != NULL) { |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 208 | rc = auth_get_param(param->pk, img_desc->parent, |
| 209 | &pk_ptr, &pk_len); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 210 | if (rc != 0) { |
| 211 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 212 | __func__, __LINE__, rc); |
| 213 | return rc; |
| 214 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 215 | } else { |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 222 | if (rc != 0) { |
| 223 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 224 | __func__, __LINE__, rc); |
| 225 | return rc; |
| 226 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 227 | |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 234 | if (rc != 0) { |
| 235 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 236 | __func__, __LINE__, rc); |
| 237 | return rc; |
| 238 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 239 | |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 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) { |
Soby Mathew | 2f7ed05 | 2016-05-24 15:05:15 +0100 | [diff] [blame] | 249 | NOTICE("ROTPK is not deployed on platform. " |
| 250 | "Skipping ROTPK verification.\n"); |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 251 | } else if ((flags & ROTPK_IS_HASH) != 0U) { |
| 252 | /* |
| 253 | * platform may store the hash of a prefixed, |
| 254 | * suffixed or modified pk |
| 255 | */ |
Robin van der Gracht | 5cea313 | 2023-09-13 10:02:38 +0200 | [diff] [blame] | 256 | 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] | 257 | if (rc != 0) { |
| 258 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 259 | __func__, __LINE__, rc); |
| 260 | return rc; |
| 261 | } |
Nicolas Toromanoff | 7f95ac8 | 2020-11-09 12:14:52 +0100 | [diff] [blame] | 262 | |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 263 | /* |
| 264 | * The hash of the certificate's public key must match |
| 265 | * the hash of the ROTPK. |
| 266 | */ |
Robin van der Gracht | 5cea313 | 2023-09-13 10:02:38 +0200 | [diff] [blame] | 267 | rc = crypto_mod_verify_hash(cnv_pk_ptr, cnv_pk_len, |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 268 | pk_plat_ptr, pk_plat_len); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 269 | if (rc != 0) { |
| 270 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 271 | __func__, __LINE__, rc); |
| 272 | return rc; |
| 273 | } |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 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 | } |
Soby Mathew | 2f7ed05 | 2016-05-24 15:05:15 +0100 | [diff] [blame] | 281 | } |
Manish V Badarkhe | caf03fd | 2023-04-11 12:55:07 +0100 | [diff] [blame] | 282 | |
| 283 | /* |
Manish V Badarkhe | caf03fd | 2023-04-11 12:55:07 +0100 | [diff] [blame] | 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) { |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 289 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 290 | __func__, __LINE__, rc); |
Manish V Badarkhe | caf03fd | 2023-04-11 12:55:07 +0100 | [diff] [blame] | 291 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 299 | if (rc != 0) { |
| 300 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 301 | __func__, __LINE__, rc); |
| 302 | return rc; |
| 303 | } |
Manish V Badarkhe | e984bc7 | 2023-03-10 19:00:02 +0000 | [diff] [blame] | 304 | |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 305 | return 0; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 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 |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 314 | * value is larger, the counter in the platform must be updated to the new value |
| 315 | * (provided it has been authenticated). |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 316 | * |
| 317 | * Return: 0 = success, Otherwise = error |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 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 |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 322 | */ |
| 323 | static int auth_nvctr(const auth_method_param_nv_ctr_t *param, |
| 324 | const auth_img_desc_t *img_desc, |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 325 | void *img, unsigned int img_len, |
| 326 | unsigned int *cert_nv_ctr, |
| 327 | bool *need_nv_ctr_upgrade) |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 328 | { |
Demi Marie Obenour | 745f6f5 | 2022-12-09 18:21:47 -0500 | [diff] [blame] | 329 | unsigned char *p; |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 330 | void *data_ptr = NULL; |
| 331 | unsigned int data_len, len, i; |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 332 | unsigned int plat_nv_ctr; |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 333 | int rc; |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 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); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 339 | if (rc != 0) { |
| 340 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 341 | __func__, __LINE__, rc); |
| 342 | return rc; |
| 343 | } |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 344 | |
| 345 | /* Parse the DER encoded integer */ |
| 346 | assert(data_ptr); |
Demi Marie Obenour | 745f6f5 | 2022-12-09 18:21:47 -0500 | [diff] [blame] | 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)) { |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 354 | /* Invalid ASN.1 integer */ |
| 355 | return 1; |
| 356 | } |
| 357 | p++; |
| 358 | |
Demi Marie Obenour | 745f6f5 | 2022-12-09 18:21:47 -0500 | [diff] [blame] | 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)) { |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 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 */ |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 375 | *cert_nv_ctr = 0; |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 376 | for (i = 0; i < len; i++) { |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 377 | *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++; |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /* Get the counter from the platform */ |
| 381 | 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] | 382 | if (rc != 0) { |
| 383 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 384 | __func__, __LINE__, rc); |
| 385 | return rc; |
| 386 | } |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 387 | |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 388 | if (*cert_nv_ctr < plat_nv_ctr) { |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 389 | /* Invalid NV-counter */ |
| 390 | return 1; |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 391 | } else if (*cert_nv_ctr > plat_nv_ctr) { |
Manish V Badarkhe | 04005b7 | 2021-06-20 22:29:22 +0100 | [diff] [blame] | 392 | #if PSA_FWU_SUPPORT && IMAGE_BL2 |
Sughosh Ganu | 84b2f7d | 2024-02-01 16:59:01 +0530 | [diff] [blame] | 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 | #else |
| 399 | *need_nv_ctr_upgrade = true; |
Manish V Badarkhe | 04005b7 | 2021-06-20 22:29:22 +0100 | [diff] [blame] | 400 | #endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */ |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
dp-arm | b3e8580 | 2016-12-12 14:48:13 +0000 | [diff] [blame] | 406 | int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused, |
| 407 | unsigned int nv_ctr) |
| 408 | { |
| 409 | return plat_set_nv_ctr(cookie, nv_ctr); |
| 410 | } |
| 411 | |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 412 | /* |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 413 | * Return the parent id in the output parameter '*parent_id' |
| 414 | * |
| 415 | * Return value: |
| 416 | * 0 = Image has parent, 1 = Image has no parent or parent is authenticated |
| 417 | */ |
| 418 | int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id) |
| 419 | { |
| 420 | const auth_img_desc_t *img_desc = NULL; |
| 421 | |
| 422 | assert(parent_id != NULL); |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 423 | /* Get the image descriptor */ |
Louis Mayencourt | 944ade8 | 2019-08-08 12:03:26 +0100 | [diff] [blame] | 424 | img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id); |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 425 | |
| 426 | /* Check if the image has no parent (ROT) */ |
| 427 | if (img_desc->parent == NULL) { |
| 428 | *parent_id = 0; |
| 429 | return 1; |
| 430 | } |
| 431 | |
| 432 | /* Check if the parent has already been authenticated */ |
| 433 | if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) { |
| 434 | *parent_id = 0; |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | *parent_id = img_desc->parent->img_id; |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Initialize the different modules in the authentication framework |
| 444 | */ |
| 445 | void auth_mod_init(void) |
| 446 | { |
| 447 | /* Check we have a valid CoT registered */ |
| 448 | assert(cot_desc_ptr != NULL); |
| 449 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 450 | /* Image parser module */ |
| 451 | img_parser_init(); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Authenticate a certificate/image |
| 456 | * |
| 457 | * Return: 0 = success, Otherwise = error |
| 458 | */ |
| 459 | int auth_mod_verify_img(unsigned int img_id, |
| 460 | void *img_ptr, |
| 461 | unsigned int img_len) |
| 462 | { |
| 463 | const auth_img_desc_t *img_desc = NULL; |
Manish V Badarkhe | caf03fd | 2023-04-11 12:55:07 +0100 | [diff] [blame] | 464 | const auth_param_type_desc_t *type_desc = NULL; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 465 | const auth_method_desc_t *auth_method = NULL; |
| 466 | void *param_ptr; |
| 467 | unsigned int param_len; |
| 468 | int rc, i; |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 469 | unsigned int cert_nv_ctr = 0; |
| 470 | bool need_nv_ctr_upgrade = false; |
| 471 | bool sig_auth_done = false; |
| 472 | const auth_method_param_nv_ctr_t *nv_ctr_param = NULL; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 473 | |
| 474 | /* Get the image descriptor from the chain of trust */ |
Louis Mayencourt | 944ade8 | 2019-08-08 12:03:26 +0100 | [diff] [blame] | 475 | img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id); |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 476 | |
| 477 | /* Ask the parser to check the image integrity */ |
| 478 | 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] | 479 | if (rc != 0) { |
| 480 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 481 | __func__, __LINE__, rc); |
| 482 | return rc; |
| 483 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 484 | |
| 485 | /* Authenticate the image using the methods indicated in the image |
| 486 | * descriptor. */ |
Joel Hutton | 8634cff | 2019-04-09 09:25:55 +0100 | [diff] [blame] | 487 | if (img_desc->img_auth_methods == NULL) |
Joel Hutton | 69931af | 2019-03-11 11:37:38 +0000 | [diff] [blame] | 488 | return 1; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 489 | for (i = 0 ; i < AUTH_METHOD_NUM ; i++) { |
| 490 | auth_method = &img_desc->img_auth_methods[i]; |
| 491 | switch (auth_method->type) { |
| 492 | case AUTH_METHOD_NONE: |
| 493 | rc = 0; |
| 494 | break; |
| 495 | case AUTH_METHOD_HASH: |
| 496 | rc = auth_hash(&auth_method->param.hash, |
| 497 | img_desc, img_ptr, img_len); |
| 498 | break; |
| 499 | case AUTH_METHOD_SIG: |
| 500 | rc = auth_signature(&auth_method->param.sig, |
| 501 | img_desc, img_ptr, img_len); |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 502 | sig_auth_done = true; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 503 | break; |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 504 | case AUTH_METHOD_NV_CTR: |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 505 | nv_ctr_param = &auth_method->param.nv_ctr; |
| 506 | rc = auth_nvctr(nv_ctr_param, |
| 507 | img_desc, img_ptr, img_len, |
| 508 | &cert_nv_ctr, &need_nv_ctr_upgrade); |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 509 | break; |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 510 | default: |
| 511 | /* Unknown authentication method */ |
| 512 | rc = 1; |
| 513 | break; |
| 514 | } |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 515 | if (rc != 0) { |
| 516 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 517 | __func__, __LINE__, rc); |
| 518 | return rc; |
| 519 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 522 | /* |
| 523 | * Do platform NV counter upgrade only if the certificate gets |
| 524 | * authenticated, and platform NV-counter upgrade is needed. |
| 525 | */ |
| 526 | if (need_nv_ctr_upgrade && sig_auth_done) { |
| 527 | rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie, |
| 528 | img_desc, cert_nv_ctr); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 529 | if (rc != 0) { |
| 530 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 531 | __func__, __LINE__, rc); |
| 532 | return rc; |
| 533 | } |
Manish V Badarkhe | 5989c56 | 2021-04-25 16:32:11 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 536 | /* Extract the parameters indicated in the image descriptor to |
| 537 | * authenticate the children images. */ |
Joel Hutton | 69931af | 2019-03-11 11:37:38 +0000 | [diff] [blame] | 538 | if (img_desc->authenticated_data != NULL) { |
| 539 | for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) { |
| 540 | if (img_desc->authenticated_data[i].type_desc == NULL) { |
| 541 | continue; |
| 542 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 543 | |
Joel Hutton | 69931af | 2019-03-11 11:37:38 +0000 | [diff] [blame] | 544 | /* Get the parameter from the image parser module */ |
| 545 | rc = img_parser_get_auth_param(img_desc->img_type, |
| 546 | img_desc->authenticated_data[i].type_desc, |
| 547 | img_ptr, img_len, ¶m_ptr, ¶m_len); |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 548 | if (rc != 0) { |
| 549 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 550 | __func__, __LINE__, rc); |
| 551 | return rc; |
| 552 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 553 | |
Joel Hutton | 69931af | 2019-03-11 11:37:38 +0000 | [diff] [blame] | 554 | /* Check parameter size */ |
| 555 | if (param_len > img_desc->authenticated_data[i].data.len) { |
| 556 | return 1; |
| 557 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 558 | |
Joel Hutton | 69931af | 2019-03-11 11:37:38 +0000 | [diff] [blame] | 559 | /* Copy the parameter for later use */ |
| 560 | memcpy((void *)img_desc->authenticated_data[i].data.ptr, |
| 561 | (void *)param_ptr, param_len); |
Manish V Badarkhe | caf03fd | 2023-04-11 12:55:07 +0100 | [diff] [blame] | 562 | |
| 563 | /* |
| 564 | * If this is a public key then measure and publicise |
| 565 | * it. |
| 566 | */ |
| 567 | type_desc = img_desc->authenticated_data[i].type_desc; |
| 568 | if (type_desc->type == AUTH_PARAM_PUB_KEY) { |
| 569 | rc = plat_mboot_measure_key(type_desc->cookie, |
| 570 | param_ptr, |
| 571 | param_len); |
| 572 | if (rc != 0) { |
Sandrine Bailleux | 9891315 | 2023-11-09 14:26:42 +0100 | [diff] [blame] | 573 | VERBOSE("[TBB] %s():%d failed with error code %d.\n", |
| 574 | __func__, __LINE__, rc); |
Manish V Badarkhe | caf03fd | 2023-04-11 12:55:07 +0100 | [diff] [blame] | 575 | } |
| 576 | } |
Joel Hutton | 69931af | 2019-03-11 11:37:38 +0000 | [diff] [blame] | 577 | } |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* Mark image as authenticated */ |
| 581 | auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED; |
| 582 | |
| 583 | return 0; |
| 584 | } |