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