1 /*
2 * Copyright (c) 2015-2021, 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
35
cmp_auth_param_type_desc(const auth_param_type_desc_t * a,const auth_param_type_desc_t * b)36 static 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 */
auth_get_param(const auth_param_type_desc_t * param_type_desc,const auth_img_desc_t * img_desc,void ** param,unsigned int * len)49 static 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
55 if (img_desc->authenticated_data == NULL)
56 return 1;
57
58 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 */
auth_hash(const auth_method_param_hash_t * param,const auth_img_desc_t * img_desc,void * img,unsigned int img_len)95 static 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 */
auth_signature(const auth_method_param_sig_t * param,const auth_img_desc_t * img_desc,void * img,unsigned int img_len)148 static 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
184 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 */
187 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
201 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 }
209 } 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 /*
221 * 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
226 * value is larger, the counter in the platform must be updated to the new value
227 * (provided it has been authenticated).
228 *
229 * Return: 0 = success, Otherwise = error
230 * 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
234 */
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)235 static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
236 const auth_img_desc_t *img_desc,
237 void *img, unsigned int img_len,
238 unsigned int *cert_nv_ctr,
239 bool *need_nv_ctr_upgrade)
240 {
241 char *p;
242 void *data_ptr = NULL;
243 unsigned int data_len, len, i;
244 unsigned int plat_nv_ctr;
245 int rc = 0;
246 bool is_trial_run = false;
247
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 */
276 *cert_nv_ctr = 0;
277 for (i = 0; i < len; i++) {
278 *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
279 }
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
285 if (*cert_nv_ctr < plat_nv_ctr) {
286 /* Invalid NV-counter */
287 return 1;
288 } else if (*cert_nv_ctr > plat_nv_ctr) {
289 #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;
293 }
294
295 return 0;
296 }
297
plat_set_nv_ctr2(void * cookie,const auth_img_desc_t * img_desc __unused,unsigned int nv_ctr)298 int 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
304 /*
305 * 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 */
auth_mod_get_parent_id(unsigned int img_id,unsigned int * parent_id)310 int 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);
315 /* Get the image descriptor */
316 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
317
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 */
auth_mod_init(void)337 void auth_mod_init(void)
338 {
339 /* Check we have a valid CoT registered */
340 assert(cot_desc_ptr != NULL);
341
342 /* Crypto module */
343 crypto_mod_init();
344
345 /* Image parser module */
346 img_parser_init();
347 }
348
349 /*
350 * Authenticate a certificate/image
351 *
352 * Return: 0 = success, Otherwise = error
353 */
auth_mod_verify_img(unsigned int img_id,void * img_ptr,unsigned int img_len)354 int auth_mod_verify_img(unsigned int img_id,
355 void *img_ptr,
356 unsigned int img_len)
357 {
358 const auth_img_desc_t *img_desc = NULL;
359 const auth_method_desc_t *auth_method = NULL;
360 void *param_ptr;
361 unsigned int param_len;
362 int rc, i;
363 unsigned int cert_nv_ctr = 0;
364 bool need_nv_ctr_upgrade = false;
365 bool sig_auth_done = false;
366 const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
367
368 /* Get the image descriptor from the chain of trust */
369 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
370
371 /* Ask the parser to check the image integrity */
372 rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
373 return_if_error(rc);
374
375 /* Authenticate the image using the methods indicated in the image
376 * descriptor. */
377 if (img_desc->img_auth_methods == NULL)
378 return 1;
379 for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
380 auth_method = &img_desc->img_auth_methods[i];
381 switch (auth_method->type) {
382 case AUTH_METHOD_NONE:
383 rc = 0;
384 break;
385 case AUTH_METHOD_HASH:
386 rc = auth_hash(&auth_method->param.hash,
387 img_desc, img_ptr, img_len);
388 break;
389 case AUTH_METHOD_SIG:
390 rc = auth_signature(&auth_method->param.sig,
391 img_desc, img_ptr, img_len);
392 sig_auth_done = true;
393 break;
394 case AUTH_METHOD_NV_CTR:
395 nv_ctr_param = &auth_method->param.nv_ctr;
396 rc = auth_nvctr(nv_ctr_param,
397 img_desc, img_ptr, img_len,
398 &cert_nv_ctr, &need_nv_ctr_upgrade);
399 break;
400 default:
401 /* Unknown authentication method */
402 rc = 1;
403 break;
404 }
405 return_if_error(rc);
406 }
407
408 /*
409 * Do platform NV counter upgrade only if the certificate gets
410 * authenticated, and platform NV-counter upgrade is needed.
411 */
412 if (need_nv_ctr_upgrade && sig_auth_done) {
413 rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
414 img_desc, cert_nv_ctr);
415 return_if_error(rc);
416 }
417
418 /* Extract the parameters indicated in the image descriptor to
419 * authenticate the children images. */
420 if (img_desc->authenticated_data != NULL) {
421 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
422 if (img_desc->authenticated_data[i].type_desc == NULL) {
423 continue;
424 }
425
426 /* Get the parameter from the image parser module */
427 rc = img_parser_get_auth_param(img_desc->img_type,
428 img_desc->authenticated_data[i].type_desc,
429 img_ptr, img_len, ¶m_ptr, ¶m_len);
430 return_if_error(rc);
431
432 /* Check parameter size */
433 if (param_len > img_desc->authenticated_data[i].data.len) {
434 return 1;
435 }
436
437 /* Copy the parameter for later use */
438 memcpy((void *)img_desc->authenticated_data[i].data.ptr,
439 (void *)param_ptr, param_len);
440 }
441 }
442
443 /* Mark image as authenticated */
444 auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
445
446 return 0;
447 }
448