blob: a99a2c70c84f9437b3a1da09fd59d32565f0adda [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Manish V Badarkhe92de80a2021-12-16 10:41:47 +00002 * Copyright (c) 2015-2022, 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
34
Joel Huttone9919bb2019-02-20 11:56:46 +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{
152 void *data_ptr, *pk_ptr, *pk_hash_ptr, *sig_ptr, *sig_alg_ptr;
153 unsigned int data_len, pk_len, pk_hash_len, sig_len, sig_alg_len;
154 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 */
175 if (img_desc->parent) {
176 rc = auth_get_param(param->pk, img_desc->parent,
177 &pk_ptr, &pk_len);
178 } else {
179 rc = plat_get_rotpk_info(param->pk->cookie, &pk_ptr, &pk_len,
180 &flags);
181 }
182 return_if_error(rc);
183
Soby Mathew2f7ed052016-05-24 15:05:15 +0100184 if (flags & (ROTPK_IS_HASH | ROTPK_NOT_DEPLOYED)) {
185 /* If the PK is a hash of the key or if the ROTPK is not
186 deployed on the platform, retrieve the key from the image */
Juan Castillo8e55d932015-04-02 09:48:16 +0100187 pk_hash_ptr = pk_ptr;
188 pk_hash_len = pk_len;
189 rc = img_parser_get_auth_param(img_desc->img_type,
190 param->pk, img, img_len,
191 &pk_ptr, &pk_len);
192 return_if_error(rc);
193
194 /* Ask the crypto module to verify the signature */
195 rc = crypto_mod_verify_signature(data_ptr, data_len,
196 sig_ptr, sig_len,
197 sig_alg_ptr, sig_alg_len,
198 pk_ptr, pk_len);
199 return_if_error(rc);
200
Soby Mathew2f7ed052016-05-24 15:05:15 +0100201 if (flags & ROTPK_NOT_DEPLOYED) {
202 NOTICE("ROTPK is not deployed on platform. "
203 "Skipping ROTPK verification.\n");
204 } else {
205 /* Ask the crypto-module to verify the key hash */
206 rc = crypto_mod_verify_hash(pk_ptr, pk_len,
207 pk_hash_ptr, pk_hash_len);
208 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100209 } else {
210 /* Ask the crypto module to verify the signature */
211 rc = crypto_mod_verify_signature(data_ptr, data_len,
212 sig_ptr, sig_len,
213 sig_alg_ptr, sig_alg_len,
214 pk_ptr, pk_len);
215 }
216
217 return rc;
218}
219
220/*
Juan Castillobfb7fa62016-01-22 11:05:57 +0000221 * Authenticate by Non-Volatile counter
222 *
223 * To protect the system against rollback, the platform includes a non-volatile
224 * counter whose value can only be increased. All certificates include a counter
225 * value that should not be lower than the value stored in the platform. If the
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100226 * value is larger, the counter in the platform must be updated to the new value
227 * (provided it has been authenticated).
Juan Castillobfb7fa62016-01-22 11:05:57 +0000228 *
229 * Return: 0 = success, Otherwise = error
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100230 * Returns additionally,
231 * cert_nv_ctr -> NV counter value present in the certificate
232 * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed
233 * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed
Juan Castillobfb7fa62016-01-22 11:05:57 +0000234 */
235static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
236 const auth_img_desc_t *img_desc,
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100237 void *img, unsigned int img_len,
238 unsigned int *cert_nv_ctr,
239 bool *need_nv_ctr_upgrade)
Juan Castillobfb7fa62016-01-22 11:05:57 +0000240{
241 char *p;
242 void *data_ptr = NULL;
243 unsigned int data_len, len, i;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100244 unsigned int plat_nv_ctr;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000245 int rc = 0;
Manish V Badarkhe04005b72021-06-20 22:29:22 +0100246 bool is_trial_run = false;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000247
248 /* Get the counter value from current image. The AM expects the IPM
249 * to return the counter value as a DER encoded integer */
250 rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
251 img, img_len, &data_ptr, &data_len);
252 return_if_error(rc);
253
254 /* Parse the DER encoded integer */
255 assert(data_ptr);
256 p = (char *)data_ptr;
257 if (*p != ASN1_INTEGER) {
258 /* Invalid ASN.1 integer */
259 return 1;
260 }
261 p++;
262
263 /* NV-counters are unsigned integers up to 32-bit */
264 len = (unsigned int)(*p & 0x7f);
265 if ((*p & 0x80) || (len > 4)) {
266 return 1;
267 }
268 p++;
269
270 /* Check the number is not negative */
271 if (*p & 0x80) {
272 return 1;
273 }
274
275 /* Convert to unsigned int. This code is for a little-endian CPU */
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100276 *cert_nv_ctr = 0;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000277 for (i = 0; i < len; i++) {
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100278 *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000279 }
280
281 /* Get the counter from the platform */
282 rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
283 return_if_error(rc);
284
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100285 if (*cert_nv_ctr < plat_nv_ctr) {
Juan Castillobfb7fa62016-01-22 11:05:57 +0000286 /* Invalid NV-counter */
287 return 1;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100288 } else if (*cert_nv_ctr > plat_nv_ctr) {
Manish V Badarkhe04005b72021-06-20 22:29:22 +0100289#if PSA_FWU_SUPPORT && IMAGE_BL2
290 is_trial_run = fwu_is_trial_run_state();
291#endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */
292 *need_nv_ctr_upgrade = !is_trial_run;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000293 }
294
295 return 0;
296}
297
dp-armb3e85802016-12-12 14:48:13 +0000298int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
299 unsigned int nv_ctr)
300{
301 return plat_set_nv_ctr(cookie, nv_ctr);
302}
303
Juan Castillobfb7fa62016-01-22 11:05:57 +0000304/*
Juan Castillo8e55d932015-04-02 09:48:16 +0100305 * Return the parent id in the output parameter '*parent_id'
306 *
307 * Return value:
308 * 0 = Image has parent, 1 = Image has no parent or parent is authenticated
309 */
310int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
311{
312 const auth_img_desc_t *img_desc = NULL;
313
314 assert(parent_id != NULL);
Juan Castillo8e55d932015-04-02 09:48:16 +0100315 /* Get the image descriptor */
Louis Mayencourt944ade82019-08-08 12:03:26 +0100316 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
Juan Castillo8e55d932015-04-02 09:48:16 +0100317
318 /* Check if the image has no parent (ROT) */
319 if (img_desc->parent == NULL) {
320 *parent_id = 0;
321 return 1;
322 }
323
324 /* Check if the parent has already been authenticated */
325 if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
326 *parent_id = 0;
327 return 1;
328 }
329
330 *parent_id = img_desc->parent->img_id;
331 return 0;
332}
333
334/*
335 * Initialize the different modules in the authentication framework
336 */
337void auth_mod_init(void)
338{
339 /* Check we have a valid CoT registered */
340 assert(cot_desc_ptr != NULL);
341
Juan Castillo8e55d932015-04-02 09:48:16 +0100342 /* Image parser module */
343 img_parser_init();
344}
345
346/*
347 * Authenticate a certificate/image
348 *
349 * Return: 0 = success, Otherwise = error
350 */
351int auth_mod_verify_img(unsigned int img_id,
352 void *img_ptr,
353 unsigned int img_len)
354{
355 const auth_img_desc_t *img_desc = NULL;
356 const auth_method_desc_t *auth_method = NULL;
357 void *param_ptr;
358 unsigned int param_len;
359 int rc, i;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100360 unsigned int cert_nv_ctr = 0;
361 bool need_nv_ctr_upgrade = false;
362 bool sig_auth_done = false;
363 const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
Juan Castillo8e55d932015-04-02 09:48:16 +0100364
365 /* Get the image descriptor from the chain of trust */
Louis Mayencourt944ade82019-08-08 12:03:26 +0100366 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
Juan Castillo8e55d932015-04-02 09:48:16 +0100367
368 /* Ask the parser to check the image integrity */
369 rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
370 return_if_error(rc);
371
372 /* Authenticate the image using the methods indicated in the image
373 * descriptor. */
Joel Hutton8634cff2019-04-09 09:25:55 +0100374 if (img_desc->img_auth_methods == NULL)
Joel Hutton69931af2019-03-11 11:37:38 +0000375 return 1;
Juan Castillo8e55d932015-04-02 09:48:16 +0100376 for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
377 auth_method = &img_desc->img_auth_methods[i];
378 switch (auth_method->type) {
379 case AUTH_METHOD_NONE:
380 rc = 0;
381 break;
382 case AUTH_METHOD_HASH:
383 rc = auth_hash(&auth_method->param.hash,
384 img_desc, img_ptr, img_len);
385 break;
386 case AUTH_METHOD_SIG:
387 rc = auth_signature(&auth_method->param.sig,
388 img_desc, img_ptr, img_len);
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100389 sig_auth_done = true;
Juan Castillo8e55d932015-04-02 09:48:16 +0100390 break;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000391 case AUTH_METHOD_NV_CTR:
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100392 nv_ctr_param = &auth_method->param.nv_ctr;
393 rc = auth_nvctr(nv_ctr_param,
394 img_desc, img_ptr, img_len,
395 &cert_nv_ctr, &need_nv_ctr_upgrade);
Juan Castillobfb7fa62016-01-22 11:05:57 +0000396 break;
Juan Castillo8e55d932015-04-02 09:48:16 +0100397 default:
398 /* Unknown authentication method */
399 rc = 1;
400 break;
401 }
402 return_if_error(rc);
403 }
404
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100405 /*
406 * Do platform NV counter upgrade only if the certificate gets
407 * authenticated, and platform NV-counter upgrade is needed.
408 */
409 if (need_nv_ctr_upgrade && sig_auth_done) {
410 rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
411 img_desc, cert_nv_ctr);
412 return_if_error(rc);
413 }
414
Juan Castillo8e55d932015-04-02 09:48:16 +0100415 /* Extract the parameters indicated in the image descriptor to
416 * authenticate the children images. */
Joel Hutton69931af2019-03-11 11:37:38 +0000417 if (img_desc->authenticated_data != NULL) {
418 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
419 if (img_desc->authenticated_data[i].type_desc == NULL) {
420 continue;
421 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100422
Joel Hutton69931af2019-03-11 11:37:38 +0000423 /* Get the parameter from the image parser module */
424 rc = img_parser_get_auth_param(img_desc->img_type,
425 img_desc->authenticated_data[i].type_desc,
426 img_ptr, img_len, &param_ptr, &param_len);
427 return_if_error(rc);
Juan Castillo8e55d932015-04-02 09:48:16 +0100428
Joel Hutton69931af2019-03-11 11:37:38 +0000429 /* Check parameter size */
430 if (param_len > img_desc->authenticated_data[i].data.len) {
431 return 1;
432 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100433
Joel Hutton69931af2019-03-11 11:37:38 +0000434 /* Copy the parameter for later use */
435 memcpy((void *)img_desc->authenticated_data[i].data.ptr,
436 (void *)param_ptr, param_len);
437 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100438 }
439
440 /* Mark image as authenticated */
441 auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
442
443 return 0;
444}