blob: f153065375bf232e250dcb7599ecef0fb6fdb049 [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Manish V Badarkhee984bc72023-03-10 19:00:02 +00002 * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
Juan Castillo8e55d932015-04-02 09:48:16 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo8e55d932015-04-02 09:48:16 +01005 */
6
7#include <assert.h>
Juan Castillo8e55d932015-04-02 09:48:16 +01008#include <stdint.h>
9#include <string.h>
10
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#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 Badarkhe04005b72021-06-20 22:29:22 +010019#include <drivers/fwu/fwu.h>
Louis Mayencourt944ade82019-08-08 12:03:26 +010020#include <lib/fconf/fconf_tbbr_getter.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021#include <plat/common/platform.h>
22
Juan Castillobfb7fa62016-01-22 11:05:57 +000023/* ASN.1 tags */
24#define ASN1_INTEGER 0x02
25
Juan Castillo8e55d932015-04-02 09:48:16 +010026#define return_if_error(rc) \
27 do { \
28 if (rc != 0) { \
29 return rc; \
30 } \
31 } while (0)
32
dp-armb3e85802016-12-12 14:48:13 +000033#pragma weak plat_set_nv_ctr2
Nicolas Toromanoff7f95ac82020-11-09 12:14:52 +010034#pragma weak plat_convert_pk
dp-armb3e85802016-12-12 14:48:13 +000035
Juan Castillo8e55d932015-04-02 09:48:16 +010036static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
37 const auth_param_type_desc_t *b)
38{
39 if ((a->type == b->type) && (a->cookie == b->cookie)) {
40 return 0;
41 }
42 return 1;
43}
44
45/*
46 * This function obtains the requested authentication parameter data from the
47 * information extracted from the parent image after its authentication.
48 */
49static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
50 const auth_img_desc_t *img_desc,
51 void **param, unsigned int *len)
52{
53 int i;
54
Joel Hutton69931af2019-03-11 11:37:38 +000055 if (img_desc->authenticated_data == NULL)
56 return 1;
57
Juan Castillo8e55d932015-04-02 09:48:16 +010058 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
59 if (0 == cmp_auth_param_type_desc(param_type_desc,
60 img_desc->authenticated_data[i].type_desc)) {
61 *param = img_desc->authenticated_data[i].data.ptr;
62 *len = img_desc->authenticated_data[i].data.len;
63 return 0;
64 }
65 }
66
67 return 1;
68}
69
70/*
71 * Authenticate an image by matching the data hash
72 *
73 * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
74 * this method, the image must contain:
75 *
76 * - The data to calculate the hash from
77 *
78 * The parent image must contain:
79 *
80 * - The hash to be matched with (including hash algorithm)
81 *
82 * For a successful authentication, both hashes must match. The function calls
83 * the crypto-module to check this matching.
84 *
85 * Parameters:
86 * param: parameters to perform the hash authentication
87 * img_desc: pointer to image descriptor so we can know the image type
88 * and parent image
89 * img: pointer to image in memory
90 * img_len: length of image (in bytes)
91 *
92 * Return:
93 * 0 = success, Otherwise = error
94 */
95static int auth_hash(const auth_method_param_hash_t *param,
96 const auth_img_desc_t *img_desc,
97 void *img, unsigned int img_len)
98{
99 void *data_ptr, *hash_der_ptr;
100 unsigned int data_len, hash_der_len;
101 int rc = 0;
102
103 /* Get the hash from the parent image. This hash will be DER encoded
104 * and contain the hash algorithm */
105 rc = auth_get_param(param->hash, img_desc->parent,
106 &hash_der_ptr, &hash_der_len);
107 return_if_error(rc);
108
109 /* Get the data to be hashed from the current image */
110 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
111 img, img_len, &data_ptr, &data_len);
112 return_if_error(rc);
113
114 /* Ask the crypto module to verify this hash */
115 rc = crypto_mod_verify_hash(data_ptr, data_len,
116 hash_der_ptr, hash_der_len);
117
118 return rc;
119}
120
121/*
122 * Authenticate by digital signature
123 *
124 * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
125 * this method, the image must contain:
126 *
127 * - Data to be signed
128 * - Signature
129 * - Signature algorithm
130 *
131 * We rely on the image parser module to extract this data from the image.
132 * The parent image must contain:
133 *
134 * - Public key (or a hash of it)
135 *
136 * If the parent image contains only a hash of the key, we will try to obtain
137 * the public key from the image itself (i.e. self-signed certificates). In that
138 * case, the signature verification is considered just an integrity check and
139 * the authentication is established by calculating the hash of the key and
140 * comparing it with the hash obtained from the parent.
141 *
142 * If the image has no parent (NULL), it means it has to be authenticated using
143 * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
144 * or a hash of it.
145 *
146 * Return: 0 = success, Otherwise = error
147 */
148static int auth_signature(const auth_method_param_sig_t *param,
149 const auth_img_desc_t *img_desc,
150 void *img, unsigned int img_len)
151{
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000152 void *data_ptr, *pk_ptr, *pk_plat_ptr, *sig_ptr, *sig_alg_ptr;
153 unsigned int data_len, pk_len, pk_plat_len, sig_len, sig_alg_len;
Juan Castillo8e55d932015-04-02 09:48:16 +0100154 unsigned int flags = 0;
155 int rc = 0;
156
157 /* Get the data to be signed from current image */
158 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
159 img, img_len, &data_ptr, &data_len);
160 return_if_error(rc);
161
162 /* Get the signature from current image */
163 rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
164 img, img_len, &sig_ptr, &sig_len);
165 return_if_error(rc);
166
167 /* Get the signature algorithm from current image */
168 rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
169 img, img_len, &sig_alg_ptr, &sig_alg_len);
170 return_if_error(rc);
171
172 /* Get the public key from the parent. If there is no parent (NULL),
173 * the certificate has been signed with the ROTPK, so we have to get
174 * the PK from the platform */
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000175 if (img_desc->parent != NULL) {
Juan Castillo8e55d932015-04-02 09:48:16 +0100176 rc = auth_get_param(param->pk, img_desc->parent,
177 &pk_ptr, &pk_len);
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000178 return_if_error(rc);
Juan Castillo8e55d932015-04-02 09:48:16 +0100179 } else {
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000180 /*
181 * Root certificates are signed with the ROTPK, so we have to
182 * get it from the platform.
183 */
184 rc = plat_get_rotpk_info(param->pk->cookie, &pk_plat_ptr,
185 &pk_plat_len, &flags);
Juan Castillo8e55d932015-04-02 09:48:16 +0100186 return_if_error(rc);
187
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000188 assert(is_rotpk_flags_valid(flags));
189
190 /* Also retrieve the key from the image. */
191 rc = img_parser_get_auth_param(img_desc->img_type,
192 param->pk, img, img_len,
193 &pk_ptr, &pk_len);
Juan Castillo8e55d932015-04-02 09:48:16 +0100194 return_if_error(rc);
195
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000196 /*
197 * Validate the certificate's key against the platform ROTPK.
198 *
199 * Platform may store key in one of the following way -
200 * 1. Hash of ROTPK
201 * 2. Hash if prefixed, suffixed or modified ROTPK
202 * 3. Full ROTPK
203 */
204 if ((flags & ROTPK_NOT_DEPLOYED) != 0U) {
Soby Mathew2f7ed052016-05-24 15:05:15 +0100205 NOTICE("ROTPK is not deployed on platform. "
206 "Skipping ROTPK verification.\n");
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000207 } else if ((flags & ROTPK_IS_HASH) != 0U) {
208 /*
209 * platform may store the hash of a prefixed,
210 * suffixed or modified pk
211 */
Nicolas Toromanoff7f95ac82020-11-09 12:14:52 +0100212 rc = plat_convert_pk(pk_ptr, pk_len, &pk_ptr, &pk_len);
213 return_if_error(rc);
214
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000215 /*
216 * The hash of the certificate's public key must match
217 * the hash of the ROTPK.
218 */
Soby Mathew2f7ed052016-05-24 15:05:15 +0100219 rc = crypto_mod_verify_hash(pk_ptr, pk_len,
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000220 pk_plat_ptr, pk_plat_len);
221 return_if_error(rc);
222 } else {
223 /* Platform supports full ROTPK */
224 if ((pk_len != pk_plat_len) ||
225 (memcmp(pk_plat_ptr, pk_ptr, pk_len) != 0)) {
226 ERROR("plat and cert ROTPK len mismatch\n");
227 return -1;
228 }
Soby Mathew2f7ed052016-05-24 15:05:15 +0100229 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100230 }
231
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000232 /* Ask the crypto module to verify the signature */
233 rc = crypto_mod_verify_signature(data_ptr, data_len,
234 sig_ptr, sig_len,
235 sig_alg_ptr, sig_alg_len,
236 pk_ptr, pk_len);
237
Juan Castillo8e55d932015-04-02 09:48:16 +0100238 return rc;
239}
240
241/*
Juan Castillobfb7fa62016-01-22 11:05:57 +0000242 * Authenticate by Non-Volatile counter
243 *
244 * To protect the system against rollback, the platform includes a non-volatile
245 * counter whose value can only be increased. All certificates include a counter
246 * value that should not be lower than the value stored in the platform. If the
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100247 * value is larger, the counter in the platform must be updated to the new value
248 * (provided it has been authenticated).
Juan Castillobfb7fa62016-01-22 11:05:57 +0000249 *
250 * Return: 0 = success, Otherwise = error
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100251 * Returns additionally,
252 * cert_nv_ctr -> NV counter value present in the certificate
253 * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed
254 * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed
Juan Castillobfb7fa62016-01-22 11:05:57 +0000255 */
256static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
257 const auth_img_desc_t *img_desc,
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100258 void *img, unsigned int img_len,
259 unsigned int *cert_nv_ctr,
260 bool *need_nv_ctr_upgrade)
Juan Castillobfb7fa62016-01-22 11:05:57 +0000261{
Demi Marie Obenour745f6f52022-12-09 18:21:47 -0500262 unsigned char *p;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000263 void *data_ptr = NULL;
264 unsigned int data_len, len, i;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100265 unsigned int plat_nv_ctr;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000266 int rc = 0;
Manish V Badarkhe04005b72021-06-20 22:29:22 +0100267 bool is_trial_run = false;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000268
269 /* Get the counter value from current image. The AM expects the IPM
270 * to return the counter value as a DER encoded integer */
271 rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
272 img, img_len, &data_ptr, &data_len);
273 return_if_error(rc);
274
275 /* Parse the DER encoded integer */
276 assert(data_ptr);
Demi Marie Obenour745f6f52022-12-09 18:21:47 -0500277 p = (unsigned char *)data_ptr;
278
279 /*
280 * Integers must be at least 3 bytes: 1 for tag, 1 for length, and 1
281 * for value. The first byte (tag) must be ASN1_INTEGER.
282 */
283 if ((data_len < 3) || (*p != ASN1_INTEGER)) {
Juan Castillobfb7fa62016-01-22 11:05:57 +0000284 /* Invalid ASN.1 integer */
285 return 1;
286 }
287 p++;
288
Demi Marie Obenour745f6f52022-12-09 18:21:47 -0500289 /*
290 * NV-counters are unsigned integers up to 31 bits. Trailing
291 * padding is not allowed.
292 */
293 len = (unsigned int)*p;
294 if ((len > 4) || (data_len - 2 != len)) {
Juan Castillobfb7fa62016-01-22 11:05:57 +0000295 return 1;
296 }
297 p++;
298
299 /* Check the number is not negative */
300 if (*p & 0x80) {
301 return 1;
302 }
303
304 /* Convert to unsigned int. This code is for a little-endian CPU */
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100305 *cert_nv_ctr = 0;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000306 for (i = 0; i < len; i++) {
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100307 *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000308 }
309
310 /* Get the counter from the platform */
311 rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
312 return_if_error(rc);
313
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100314 if (*cert_nv_ctr < plat_nv_ctr) {
Juan Castillobfb7fa62016-01-22 11:05:57 +0000315 /* Invalid NV-counter */
316 return 1;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100317 } else if (*cert_nv_ctr > plat_nv_ctr) {
Manish V Badarkhe04005b72021-06-20 22:29:22 +0100318#if PSA_FWU_SUPPORT && IMAGE_BL2
319 is_trial_run = fwu_is_trial_run_state();
320#endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */
321 *need_nv_ctr_upgrade = !is_trial_run;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000322 }
323
324 return 0;
325}
326
dp-armb3e85802016-12-12 14:48:13 +0000327int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
328 unsigned int nv_ctr)
329{
330 return plat_set_nv_ctr(cookie, nv_ctr);
331}
332
Nicolas Toromanoff7f95ac82020-11-09 12:14:52 +0100333int plat_convert_pk(void *full_pk_ptr, unsigned int full_pk_len,
334 void **hashed_pk_ptr, unsigned int *hashed_pk_len)
335{
336 *hashed_pk_ptr = full_pk_ptr;
337 *hashed_pk_len = full_pk_len;
338
339 return 0;
340}
341
Juan Castillobfb7fa62016-01-22 11:05:57 +0000342/*
Juan Castillo8e55d932015-04-02 09:48:16 +0100343 * Return the parent id in the output parameter '*parent_id'
344 *
345 * Return value:
346 * 0 = Image has parent, 1 = Image has no parent or parent is authenticated
347 */
348int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
349{
350 const auth_img_desc_t *img_desc = NULL;
351
352 assert(parent_id != NULL);
Juan Castillo8e55d932015-04-02 09:48:16 +0100353 /* Get the image descriptor */
Louis Mayencourt944ade82019-08-08 12:03:26 +0100354 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
Juan Castillo8e55d932015-04-02 09:48:16 +0100355
356 /* Check if the image has no parent (ROT) */
357 if (img_desc->parent == NULL) {
358 *parent_id = 0;
359 return 1;
360 }
361
362 /* Check if the parent has already been authenticated */
363 if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
364 *parent_id = 0;
365 return 1;
366 }
367
368 *parent_id = img_desc->parent->img_id;
369 return 0;
370}
371
372/*
373 * Initialize the different modules in the authentication framework
374 */
375void auth_mod_init(void)
376{
377 /* Check we have a valid CoT registered */
378 assert(cot_desc_ptr != NULL);
379
Juan Castillo8e55d932015-04-02 09:48:16 +0100380 /* Image parser module */
381 img_parser_init();
382}
383
384/*
385 * Authenticate a certificate/image
386 *
387 * Return: 0 = success, Otherwise = error
388 */
389int auth_mod_verify_img(unsigned int img_id,
390 void *img_ptr,
391 unsigned int img_len)
392{
393 const auth_img_desc_t *img_desc = NULL;
394 const auth_method_desc_t *auth_method = NULL;
395 void *param_ptr;
396 unsigned int param_len;
397 int rc, i;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100398 unsigned int cert_nv_ctr = 0;
399 bool need_nv_ctr_upgrade = false;
400 bool sig_auth_done = false;
401 const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
Juan Castillo8e55d932015-04-02 09:48:16 +0100402
403 /* Get the image descriptor from the chain of trust */
Louis Mayencourt944ade82019-08-08 12:03:26 +0100404 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
Juan Castillo8e55d932015-04-02 09:48:16 +0100405
406 /* Ask the parser to check the image integrity */
407 rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
408 return_if_error(rc);
409
410 /* Authenticate the image using the methods indicated in the image
411 * descriptor. */
Joel Hutton8634cff2019-04-09 09:25:55 +0100412 if (img_desc->img_auth_methods == NULL)
Joel Hutton69931af2019-03-11 11:37:38 +0000413 return 1;
Juan Castillo8e55d932015-04-02 09:48:16 +0100414 for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
415 auth_method = &img_desc->img_auth_methods[i];
416 switch (auth_method->type) {
417 case AUTH_METHOD_NONE:
418 rc = 0;
419 break;
420 case AUTH_METHOD_HASH:
421 rc = auth_hash(&auth_method->param.hash,
422 img_desc, img_ptr, img_len);
423 break;
424 case AUTH_METHOD_SIG:
425 rc = auth_signature(&auth_method->param.sig,
426 img_desc, img_ptr, img_len);
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100427 sig_auth_done = true;
Juan Castillo8e55d932015-04-02 09:48:16 +0100428 break;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000429 case AUTH_METHOD_NV_CTR:
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100430 nv_ctr_param = &auth_method->param.nv_ctr;
431 rc = auth_nvctr(nv_ctr_param,
432 img_desc, img_ptr, img_len,
433 &cert_nv_ctr, &need_nv_ctr_upgrade);
Juan Castillobfb7fa62016-01-22 11:05:57 +0000434 break;
Juan Castillo8e55d932015-04-02 09:48:16 +0100435 default:
436 /* Unknown authentication method */
437 rc = 1;
438 break;
439 }
440 return_if_error(rc);
441 }
442
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100443 /*
444 * Do platform NV counter upgrade only if the certificate gets
445 * authenticated, and platform NV-counter upgrade is needed.
446 */
447 if (need_nv_ctr_upgrade && sig_auth_done) {
448 rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
449 img_desc, cert_nv_ctr);
450 return_if_error(rc);
451 }
452
Juan Castillo8e55d932015-04-02 09:48:16 +0100453 /* Extract the parameters indicated in the image descriptor to
454 * authenticate the children images. */
Joel Hutton69931af2019-03-11 11:37:38 +0000455 if (img_desc->authenticated_data != NULL) {
456 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
457 if (img_desc->authenticated_data[i].type_desc == NULL) {
458 continue;
459 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100460
Joel Hutton69931af2019-03-11 11:37:38 +0000461 /* Get the parameter from the image parser module */
462 rc = img_parser_get_auth_param(img_desc->img_type,
463 img_desc->authenticated_data[i].type_desc,
464 img_ptr, img_len, &param_ptr, &param_len);
465 return_if_error(rc);
Juan Castillo8e55d932015-04-02 09:48:16 +0100466
Joel Hutton69931af2019-03-11 11:37:38 +0000467 /* Check parameter size */
468 if (param_len > img_desc->authenticated_data[i].data.len) {
469 return 1;
470 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100471
Joel Hutton69931af2019-03-11 11:37:38 +0000472 /* Copy the parameter for later use */
473 memcpy((void *)img_desc->authenticated_data[i].data.ptr,
474 (void *)param_ptr, param_len);
475 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100476 }
477
478 /* Mark image as authenticated */
479 auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
480
481 return 0;
482}