xref: /rk3399_ARM-atf/drivers/auth/auth_mod.c (revision 67748e4827976f3b13f8bc1281b3c4b59da87e4a)
1 /*
2  * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <assert.h>
32 #include <auth_common.h>
33 #include <auth_mod.h>
34 #include <cot_def.h>
35 #include <crypto_mod.h>
36 #include <debug.h>
37 #include <img_parser_mod.h>
38 #include <platform.h>
39 #include <platform_def.h>
40 #include <stdint.h>
41 #include <string.h>
42 
43 /* ASN.1 tags */
44 #define ASN1_INTEGER                 0x02
45 
46 #define return_if_error(rc) \
47 	do { \
48 		if (rc != 0) { \
49 			return rc; \
50 		} \
51 	} while (0)
52 
53 #pragma weak plat_set_nv_ctr2
54 
55 /* Pointer to CoT */
56 extern const auth_img_desc_t *const cot_desc_ptr;
57 extern unsigned int auth_img_flags[];
58 
59 static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
60 		const auth_param_type_desc_t *b)
61 {
62 	if ((a->type == b->type) && (a->cookie == b->cookie)) {
63 		return 0;
64 	}
65 	return 1;
66 }
67 
68 /*
69  * This function obtains the requested authentication parameter data from the
70  * information extracted from the parent image after its authentication.
71  */
72 static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
73 			  const auth_img_desc_t *img_desc,
74 			  void **param, unsigned int *len)
75 {
76 	int i;
77 
78 	for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
79 		if (0 == cmp_auth_param_type_desc(param_type_desc,
80 				img_desc->authenticated_data[i].type_desc)) {
81 			*param = img_desc->authenticated_data[i].data.ptr;
82 			*len = img_desc->authenticated_data[i].data.len;
83 			return 0;
84 		}
85 	}
86 
87 	return 1;
88 }
89 
90 /*
91  * Authenticate an image by matching the data hash
92  *
93  * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
94  * this method, the image must contain:
95  *
96  *   - The data to calculate the hash from
97  *
98  * The parent image must contain:
99  *
100  *   - The hash to be matched with (including hash algorithm)
101  *
102  * For a successful authentication, both hashes must match. The function calls
103  * the crypto-module to check this matching.
104  *
105  * Parameters:
106  *   param: parameters to perform the hash authentication
107  *   img_desc: pointer to image descriptor so we can know the image type
108  *             and parent image
109  *   img: pointer to image in memory
110  *   img_len: length of image (in bytes)
111  *
112  * Return:
113  *   0 = success, Otherwise = error
114  */
115 static int auth_hash(const auth_method_param_hash_t *param,
116 		     const auth_img_desc_t *img_desc,
117 		     void *img, unsigned int img_len)
118 {
119 	void *data_ptr, *hash_der_ptr;
120 	unsigned int data_len, hash_der_len;
121 	int rc = 0;
122 
123 	/* Get the hash from the parent image. This hash will be DER encoded
124 	 * and contain the hash algorithm */
125 	rc = auth_get_param(param->hash, img_desc->parent,
126 			&hash_der_ptr, &hash_der_len);
127 	return_if_error(rc);
128 
129 	/* Get the data to be hashed from the current image */
130 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
131 			img, img_len, &data_ptr, &data_len);
132 	return_if_error(rc);
133 
134 	/* Ask the crypto module to verify this hash */
135 	rc = crypto_mod_verify_hash(data_ptr, data_len,
136 				    hash_der_ptr, hash_der_len);
137 
138 	return rc;
139 }
140 
141 /*
142  * Authenticate by digital signature
143  *
144  * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
145  * this method, the image must contain:
146  *
147  *   - Data to be signed
148  *   - Signature
149  *   - Signature algorithm
150  *
151  * We rely on the image parser module to extract this data from the image.
152  * The parent image must contain:
153  *
154  *   - Public key (or a hash of it)
155  *
156  * If the parent image contains only a hash of the key, we will try to obtain
157  * the public key from the image itself (i.e. self-signed certificates). In that
158  * case, the signature verification is considered just an integrity check and
159  * the authentication is established by calculating the hash of the key and
160  * comparing it with the hash obtained from the parent.
161  *
162  * If the image has no parent (NULL), it means it has to be authenticated using
163  * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
164  * or a hash of it.
165  *
166  * Return: 0 = success, Otherwise = error
167  */
168 static int auth_signature(const auth_method_param_sig_t *param,
169 			  const auth_img_desc_t *img_desc,
170 			  void *img, unsigned int img_len)
171 {
172 	void *data_ptr, *pk_ptr, *pk_hash_ptr, *sig_ptr, *sig_alg_ptr;
173 	unsigned int data_len, pk_len, pk_hash_len, sig_len, sig_alg_len;
174 	unsigned int flags = 0;
175 	int rc = 0;
176 
177 	/* Get the data to be signed from current image */
178 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
179 			img, img_len, &data_ptr, &data_len);
180 	return_if_error(rc);
181 
182 	/* Get the signature from current image */
183 	rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
184 			img, img_len, &sig_ptr, &sig_len);
185 	return_if_error(rc);
186 
187 	/* Get the signature algorithm from current image */
188 	rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
189 			img, img_len, &sig_alg_ptr, &sig_alg_len);
190 	return_if_error(rc);
191 
192 	/* Get the public key from the parent. If there is no parent (NULL),
193 	 * the certificate has been signed with the ROTPK, so we have to get
194 	 * the PK from the platform */
195 	if (img_desc->parent) {
196 		rc = auth_get_param(param->pk, img_desc->parent,
197 				&pk_ptr, &pk_len);
198 	} else {
199 		rc = plat_get_rotpk_info(param->pk->cookie, &pk_ptr, &pk_len,
200 				&flags);
201 	}
202 	return_if_error(rc);
203 
204 	if (flags & (ROTPK_IS_HASH | ROTPK_NOT_DEPLOYED)) {
205 		/* If the PK is a hash of the key or if the ROTPK is not
206 		   deployed on the platform, retrieve the key from the image */
207 		pk_hash_ptr = pk_ptr;
208 		pk_hash_len = pk_len;
209 		rc = img_parser_get_auth_param(img_desc->img_type,
210 					param->pk, img, img_len,
211 					&pk_ptr, &pk_len);
212 		return_if_error(rc);
213 
214 		/* Ask the crypto module to verify the signature */
215 		rc = crypto_mod_verify_signature(data_ptr, data_len,
216 						 sig_ptr, sig_len,
217 						 sig_alg_ptr, sig_alg_len,
218 						 pk_ptr, pk_len);
219 		return_if_error(rc);
220 
221 		if (flags & ROTPK_NOT_DEPLOYED) {
222 			NOTICE("ROTPK is not deployed on platform. "
223 				"Skipping ROTPK verification.\n");
224 		} else {
225 			/* Ask the crypto-module to verify the key hash */
226 			rc = crypto_mod_verify_hash(pk_ptr, pk_len,
227 				    pk_hash_ptr, pk_hash_len);
228 		}
229 	} else {
230 		/* Ask the crypto module to verify the signature */
231 		rc = crypto_mod_verify_signature(data_ptr, data_len,
232 						 sig_ptr, sig_len,
233 						 sig_alg_ptr, sig_alg_len,
234 						 pk_ptr, pk_len);
235 	}
236 
237 	return rc;
238 }
239 
240 /*
241  * Authenticate by Non-Volatile counter
242  *
243  * To protect the system against rollback, the platform includes a non-volatile
244  * counter whose value can only be increased. All certificates include a counter
245  * value that should not be lower than the value stored in the platform. If the
246  * value is larger, the counter in the platform must be updated to the new
247  * value.
248  *
249  * Return: 0 = success, Otherwise = error
250  */
251 static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
252 		      const auth_img_desc_t *img_desc,
253 		      void *img, unsigned int img_len)
254 {
255 	char *p;
256 	void *data_ptr = NULL;
257 	unsigned int data_len, len, i;
258 	unsigned int cert_nv_ctr, plat_nv_ctr;
259 	int rc = 0;
260 
261 	/* Get the counter value from current image. The AM expects the IPM
262 	 * to return the counter value as a DER encoded integer */
263 	rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
264 				       img, img_len, &data_ptr, &data_len);
265 	return_if_error(rc);
266 
267 	/* Parse the DER encoded integer */
268 	assert(data_ptr);
269 	p = (char *)data_ptr;
270 	if (*p != ASN1_INTEGER) {
271 		/* Invalid ASN.1 integer */
272 		return 1;
273 	}
274 	p++;
275 
276 	/* NV-counters are unsigned integers up to 32-bit */
277 	len = (unsigned int)(*p & 0x7f);
278 	if ((*p & 0x80) || (len > 4)) {
279 		return 1;
280 	}
281 	p++;
282 
283 	/* Check the number is not negative */
284 	if (*p & 0x80) {
285 		return 1;
286 	}
287 
288 	/* Convert to unsigned int. This code is for a little-endian CPU */
289 	cert_nv_ctr = 0;
290 	for (i = 0; i < len; i++) {
291 		cert_nv_ctr = (cert_nv_ctr << 8) | *p++;
292 	}
293 
294 	/* Get the counter from the platform */
295 	rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
296 	return_if_error(rc);
297 
298 	if (cert_nv_ctr < plat_nv_ctr) {
299 		/* Invalid NV-counter */
300 		return 1;
301 	} else if (cert_nv_ctr > plat_nv_ctr) {
302 		rc = plat_set_nv_ctr2(param->plat_nv_ctr->cookie,
303 			img_desc, cert_nv_ctr);
304 		return_if_error(rc);
305 	}
306 
307 	return 0;
308 }
309 
310 int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
311 		unsigned int nv_ctr)
312 {
313 	return plat_set_nv_ctr(cookie, nv_ctr);
314 }
315 
316 /*
317  * Return the parent id in the output parameter '*parent_id'
318  *
319  * Return value:
320  *   0 = Image has parent, 1 = Image has no parent or parent is authenticated
321  */
322 int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
323 {
324 	const auth_img_desc_t *img_desc = NULL;
325 
326 	assert(parent_id != NULL);
327 
328 	/* Get the image descriptor */
329 	img_desc = &cot_desc_ptr[img_id];
330 
331 	/* Check if the image has no parent (ROT) */
332 	if (img_desc->parent == NULL) {
333 		*parent_id = 0;
334 		return 1;
335 	}
336 
337 	/* Check if the parent has already been authenticated */
338 	if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
339 		*parent_id = 0;
340 		return 1;
341 	}
342 
343 	*parent_id = img_desc->parent->img_id;
344 	return 0;
345 }
346 
347 /*
348  * Initialize the different modules in the authentication framework
349  */
350 void auth_mod_init(void)
351 {
352 	/* Check we have a valid CoT registered */
353 	assert(cot_desc_ptr != NULL);
354 
355 	/* Crypto module */
356 	crypto_mod_init();
357 
358 	/* Image parser module */
359 	img_parser_init();
360 }
361 
362 /*
363  * Authenticate a certificate/image
364  *
365  * Return: 0 = success, Otherwise = error
366  */
367 int auth_mod_verify_img(unsigned int img_id,
368 			void *img_ptr,
369 			unsigned int img_len)
370 {
371 	const auth_img_desc_t *img_desc = NULL;
372 	const auth_method_desc_t *auth_method = NULL;
373 	void *param_ptr;
374 	unsigned int param_len;
375 	int rc, i;
376 
377 	/* Get the image descriptor from the chain of trust */
378 	img_desc = &cot_desc_ptr[img_id];
379 
380 	/* Ask the parser to check the image integrity */
381 	rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
382 	return_if_error(rc);
383 
384 	/* Authenticate the image using the methods indicated in the image
385 	 * descriptor. */
386 	for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
387 		auth_method = &img_desc->img_auth_methods[i];
388 		switch (auth_method->type) {
389 		case AUTH_METHOD_NONE:
390 			rc = 0;
391 			break;
392 		case AUTH_METHOD_HASH:
393 			rc = auth_hash(&auth_method->param.hash,
394 					img_desc, img_ptr, img_len);
395 			break;
396 		case AUTH_METHOD_SIG:
397 			rc = auth_signature(&auth_method->param.sig,
398 					img_desc, img_ptr, img_len);
399 			break;
400 		case AUTH_METHOD_NV_CTR:
401 			rc = auth_nvctr(&auth_method->param.nv_ctr,
402 					img_desc, img_ptr, img_len);
403 			break;
404 		default:
405 			/* Unknown authentication method */
406 			rc = 1;
407 			break;
408 		}
409 		return_if_error(rc);
410 	}
411 
412 	/* Extract the parameters indicated in the image descriptor to
413 	 * authenticate the children images. */
414 	for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
415 		if (img_desc->authenticated_data[i].type_desc == NULL) {
416 			continue;
417 		}
418 
419 		/* Get the parameter from the image parser module */
420 		rc = img_parser_get_auth_param(img_desc->img_type,
421 				img_desc->authenticated_data[i].type_desc,
422 				img_ptr, img_len, &param_ptr, &param_len);
423 		return_if_error(rc);
424 
425 		/* Check parameter size */
426 		if (param_len > img_desc->authenticated_data[i].data.len) {
427 			return 1;
428 		}
429 
430 		/* Copy the parameter for later use */
431 		memcpy((void *)img_desc->authenticated_data[i].data.ptr,
432 				(void *)param_ptr, param_len);
433 	}
434 
435 	/* Mark image as authenticated */
436 	auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
437 
438 	return 0;
439 }
440