aboutsummaryrefslogtreecommitdiff
path: root/lib/fconf/fconf_cot_getter.c
blob: adfa5341c925d3c24fc0d66467ecab098411dd59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * Copyright (c) 2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <stddef.h>

#include <common/fdt_wrappers.h>
#include <drivers/auth/mbedtls/mbedtls_config.h>
#include <drivers/auth/auth_mod.h>
#include <lib/fconf/fconf.h>
#include <lib/object_pool.h>
#include <libfdt.h>

#include <tools_share/tbbr_oid.h>

/* static structures used during authentication process */
static auth_param_type_desc_t sig = AUTH_PARAM_TYPE_DESC(
			AUTH_PARAM_SIG, 0);
static auth_param_type_desc_t sig_alg = AUTH_PARAM_TYPE_DESC(
			AUTH_PARAM_SIG_ALG, 0);
static auth_param_type_desc_t raw_data = AUTH_PARAM_TYPE_DESC(
			AUTH_PARAM_RAW_DATA, 0);

/* pointers to an array of CoT descriptors */
static const auth_img_desc_t *cot_desc[MAX_NUMBER_IDS];
/* array of CoT descriptors */
static auth_img_desc_t auth_img_descs[MAX_NUMBER_IDS];

/* array of authentication methods structures */
static auth_method_desc_t auth_methods[MAX_NUMBER_IDS * AUTH_METHOD_NUM];
static OBJECT_POOL_ARRAY(auth_methods_pool, auth_methods);

/* array of authentication params structures */
static auth_param_desc_t auth_params[MAX_NUMBER_IDS * COT_MAX_VERIFIED_PARAMS];
static OBJECT_POOL_ARRAY(auth_params_pool, auth_params);

/* array of authentication param type structures */
static auth_param_type_desc_t auth_param_type_descs[MAX_NUMBER_IDS];
static OBJECT_POOL_ARRAY(auth_param_type_descs_pool, auth_param_type_descs);

/*
 * array of OIDs
 * Object IDs are used to search hash, pk, counter values in certificate.
 * As per binding we have below 2 combinations:
 * 1. Certificates are validated using nv-cntr and pk
 * 2. Raw images are authenticated using hash
 * Hence in worst case, there are maximum 2 OIDs per image/certificate
 */
static unsigned char oids[(MAX_NUMBER_IDS * 2)][MAX_OID_NAME_LEN];
static OBJECT_POOL_ARRAY(oid_pool, oids);

/* An array of auth buffer which holds hashes and pk
 * ToDo: Size decided with the current number of images and
 * certificates which are available in CoT. Size of these buffers bound to
 * increase in the future on the addition of images/certificates.
 */
static unsigned char hash_auth_bufs[20][HASH_DER_LEN];
static OBJECT_POOL_ARRAY(hash_auth_buf_pool, hash_auth_bufs);
static unsigned char pk_auth_bufs[12][PK_DER_LEN];
static OBJECT_POOL_ARRAY(pk_auth_buf_pool, pk_auth_bufs);

/*******************************************************************************
 * update_parent_auth_data() - Update authentication data structure
 * @auth_desc[in]:	Pointer to the auth image descriptor
 * @type_desc[in]:	Pointer to authentication parameter
 * @auth_buf_size[in]:	Buffer size to hold pk or hash
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int update_parent_auth_data(const auth_img_desc_t *auth_desc,
				   auth_param_type_desc_t *type_desc,
				   unsigned int auth_buf_size)
{
	unsigned int i;
	auth_param_desc_t *auth_data = &auth_desc->authenticated_data[0];
	unsigned char *auth_buf;

	for (i = 0U; i < COT_MAX_VERIFIED_PARAMS; i++) {
		if (auth_data[i].type_desc == type_desc) {
			return 0;
		}
		if (auth_data[i].type_desc == NULL) {
			break;
		}
	}

	if (auth_buf_size == HASH_DER_LEN) {
		auth_buf = pool_alloc(&hash_auth_buf_pool);
	} else if (auth_buf_size == PK_DER_LEN) {
		auth_buf = pool_alloc(&pk_auth_buf_pool);
	} else {
		return -1;
	}

	if (i < COT_MAX_VERIFIED_PARAMS) {
		auth_data[i].type_desc = type_desc;
		auth_data[i].data.ptr = auth_buf;
		auth_data[i].data.len = auth_buf_size;
	} else {
		ERROR("Out of authentication data array\n");
		return -1;
	}

	return 0;
}

/*******************************************************************************
 * get_auth_param_type_desc() - Get pointer of authentication parameter
 * @img_id[in]:		Image Id
 * @type_desc[out]:	Pointer to authentication parameter
 * @buf_size[out]:	Buffer size which hold hash/pk
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int get_auth_param_type_desc(unsigned int img_id,
				    auth_param_type_desc_t **type_desc,
				    unsigned int *buf_size)
{
	auth_method_desc_t *img_auth_method = NULL;
	img_type_t type = auth_img_descs[img_id].img_type;

	if (type == IMG_CERT) {
		img_auth_method =
		&auth_img_descs[img_id].img_auth_methods[AUTH_METHOD_SIG];
		*type_desc = img_auth_method->param.sig.pk;
		*buf_size = PK_DER_LEN;
	} else if (type == IMG_RAW) {
		img_auth_method =
		&auth_img_descs[img_id].img_auth_methods[AUTH_METHOD_HASH];
		*type_desc = img_auth_method->param.hash.hash;
		*buf_size = HASH_DER_LEN;
	} else {
		return -1;
	}

	return 0;
}

/*******************************************************************************
 * set_auth_method() - Update global auth image descriptors with authentication
 *			method data
 * @auth_method_type[in]:	Type of authentication method
 * @oid[in]:			Object Idetifier for pk/hash search
 * @auth_method[in]:		Pointer to authentication method to set
 ******************************************************************************/
static void set_auth_method(auth_method_type_t auth_method_type, char *oid,
			    auth_method_desc_t *auth_method)
{
	auth_param_type_t auth_param_type = AUTH_PARAM_NONE;
	auth_param_type_desc_t *auth_param_type_desc;

	assert(auth_method != NULL);

	auth_param_type_desc = pool_alloc(&auth_param_type_descs_pool);
	auth_method->type = auth_method_type;

	if (auth_method_type == AUTH_METHOD_SIG) {
		auth_param_type = AUTH_PARAM_PUB_KEY;
		auth_method->param.sig.sig = &sig;
		auth_method->param.sig.alg = &sig_alg;
		auth_method->param.sig.data = &raw_data;
		auth_method->param.sig.pk = auth_param_type_desc;
	} else if (auth_method_type == AUTH_METHOD_HASH) {
		auth_param_type = AUTH_PARAM_HASH;
		auth_method->param.hash.data = &raw_data;
		auth_method->param.hash.hash = auth_param_type_desc;
	} else if (auth_method_type == AUTH_METHOD_NV_CTR) {
		auth_param_type = AUTH_PARAM_NV_CTR;
		auth_method->param.nv_ctr.cert_nv_ctr = auth_param_type_desc;
		auth_method->param.nv_ctr.plat_nv_ctr = auth_param_type_desc;
	}

	auth_param_type_desc->type = auth_param_type;
	auth_param_type_desc->cookie = (void *)oid;
}

/*******************************************************************************
 * get_oid() -	get object identifier from device tree
 * @dtb[in]:	Pointer to the device tree blob in memory
 * @node[in]:	Offset of the node
 * @prop[in]:	Property to read from the given node
 * @oid[out]:	Object Indentifier of key/hash/nv-counter in certificate
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int get_oid(const void *dtb, int node, const char *prop, char **oid)
{
	uint32_t phandle;
	int rc;

	rc = fdt_read_uint32(dtb, node, prop, &phandle);
	if (rc < 0) {
		return rc;
	}

	node = fdt_node_offset_by_phandle(dtb, phandle);
	if (node < 0) {
		return node;
	}

	*oid = pool_alloc(&oid_pool);
	rc = fdtw_read_string(dtb, node, "oid", *oid, MAX_OID_NAME_LEN);

	return rc;
}

/*******************************************************************************
 * populate_and_set_auth_methods() -  Populate auth method parameters from
 *			device tree and set authentication method
 *			structure.
 * @dtb[in]:		Pointer to the device tree blob in memory
 * @node[in]:		Offset of the node
 * @img_id[in]:		Image identifier
 * @type[in]:		Type of image
 * @root_certificate[in]:Root certificate (authenticated by ROTPK)
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int populate_and_set_auth_methods(const void *dtb, int node,
					 unsigned int img_id, img_type_t type,
					 bool root_certificate)
{
	auth_method_type_t auth_method_type = AUTH_METHOD_NONE;
	int rc;
	char *oid = NULL;

	auth_method_desc_t *auth_method = pool_alloc_n(&auth_methods_pool,
					AUTH_METHOD_NUM);

	/*
	 * This is as per binding document where certificates are
	 * verified by signature and images are verified by hash.
	 */
	if (type == IMG_CERT) {
		if (root_certificate) {
			oid = NULL;
		} else {
			rc = get_oid(dtb, node, "signing-key", &oid);
			if (rc < 0) {
				ERROR("FCONF: Can't read %s property\n",
					"signing-key");
				return rc;
			}
		}
		auth_method_type = AUTH_METHOD_SIG;
	} else if (type == IMG_RAW) {
		rc = get_oid(dtb, node, "hash", &oid);
		if (rc < 0) {
			ERROR("FCONF: Can't read %s property\n",
				"hash");
			return rc;
		}
		auth_method_type = AUTH_METHOD_HASH;
	} else {
		return -1;
	}

	set_auth_method(auth_method_type, oid,
			&auth_method[auth_method_type]);

	/* Retrieve the optional property */
	rc = get_oid(dtb, node, "antirollback-counter", &oid);
	if (rc == 0) {
		auth_method_type = AUTH_METHOD_NV_CTR;
		set_auth_method(auth_method_type, oid,
				&auth_method[auth_method_type]);
	}

	auth_img_descs[img_id].img_auth_methods = &auth_method[0];

	return 0;
}

/*******************************************************************************
 * get_parent_img_id() - Get parent image id for given child node
 * @dtb[in]:		Pointer to the device tree blob in memory
 * @node[in]:		Offset of the child node
 * @parent_img_id[out]:	Image id of parent
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int get_parent_img_id(const void *dtb, int node,
			     unsigned int *parent_img_id)
{
	uint32_t phandle;
	int err;

	err = fdt_read_uint32(dtb, node, "parent", &phandle);
	if (err < 0) {
		ERROR("FCONF: Could not read %s property in node\n",
			"parent");
		return err;
	}

	node = fdt_node_offset_by_phandle(dtb, phandle);
	if (node < 0) {
		ERROR("FCONF: Failed to locate node using its phandle\n");
		return node;
	}

	err = fdt_read_uint32(dtb, node, "image-id", parent_img_id);
	if (err < 0) {
		ERROR("FCONF: Could not read %s property in node\n",
			"image-id");
	}

	return err;
}

/*******************************************************************************
 * set_desc_data() - Update data in descriptor's structure
 * @dtb[in]:	Pointer to the device tree blob in memory
 * @node[in]:	Offset of the node
 * @type[in]:	Type of image (RAW/CERT)
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int set_desc_data(const void *dtb, int node, img_type_t type)
{
	int rc;
	bool root_certificate = false;
	unsigned int img_id, parent_img_id;

	rc = fdt_read_uint32(dtb, node, "image-id", &img_id);
	if (rc < 0) {
		ERROR("FCONF: Can't find property %s in node\n",
			"image-id");
		return rc;
	}

	if (fdt_getprop(dtb, node, "root-certificate",
					NULL) != NULL) {
		root_certificate = true;
	}

	if (!root_certificate) {
		rc = get_parent_img_id(dtb, node, &parent_img_id);
		if (rc < 0) {
			return rc;
		}
		auth_img_descs[img_id].parent = &auth_img_descs[parent_img_id];
	}

	auth_img_descs[img_id].img_id = img_id;
	auth_img_descs[img_id].img_type = type;

	rc = populate_and_set_auth_methods(dtb, node, img_id, type,
				root_certificate);
	if (rc < 0) {
		return rc;
	}

	if (type == IMG_CERT) {
		auth_param_desc_t *auth_param =
			pool_alloc_n(&auth_params_pool,
					COT_MAX_VERIFIED_PARAMS);
		auth_img_descs[img_id].authenticated_data = &auth_param[0];
	}

	cot_desc[img_id] = &auth_img_descs[img_id];

	return rc;
}

/*******************************************************************************
 * populate_manifest_descs() - Populate CoT descriptors and update global
 *			       certificate structures
 * @dtb[in]:	Pointer to the device tree blob in memory
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int populate_manifest_descs(const void *dtb)
{
	int node, child;
	int rc;

	/*
	 * Assert the node offset points to "arm, cert-descs"
	 * compatible property
	 */
	const char *compatible_str = "arm, cert-descs";

	node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
	if (node < 0) {
		ERROR("FCONF: Can't find %s compatible in node\n",
			compatible_str);
		return node;
	}

	fdt_for_each_subnode(child, dtb, node) {
		rc = set_desc_data(dtb, child, IMG_CERT);
		if (rc < 0) {
			return rc;
		}
	}

	return 0;
}

/*******************************************************************************
 * populate_image_descs() - Populate CoT descriptors and update global
 *			    image descriptor structures.
 * @dtb[in]:	Pointer to the device tree blob in memory
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int populate_image_descs(const void *dtb)
{
	int node, child;
	int rc;

	/*
	 * Assert the node offset points to "arm, img-descs"
	 * compatible property
	 */
	const char *compatible_str = "arm, img-descs";

	node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
	if (node < 0) {
		ERROR("FCONF: Can't find %s compatible in node\n",
			compatible_str);
		return node;
	}

	fdt_for_each_subnode(child, dtb, node) {
		rc = set_desc_data(dtb, child, IMG_RAW);
		if (rc < 0) {
			return rc;
		}
	}

	return 0;
}

/*******************************************************************************
 * fconf_populate_cot_descs() - Populate CoT descriptors and update global
 *				structures
 * @config[in]:	Pointer to the device tree blob in memory
 *
 * Return 0 on success or an error value otherwise.
 ******************************************************************************/
static int fconf_populate_cot_descs(uintptr_t config)
{
	auth_param_type_desc_t *type_desc = NULL;
	unsigned int auth_buf_size = 0U;
	int rc;

	/* As libfdt uses void *, we can't avoid this cast */
	const void *dtb = (void *)config;

	/* populate manifest descs information */
	rc = populate_manifest_descs(dtb);
	if (rc < 0) {
		ERROR("FCONF: population of %s descs failed %d\n",
			"manifest", rc);
		return rc;
	}

	/* populate image descs information */
	rc = populate_image_descs(dtb);
	if (rc < 0) {
		ERROR("FCONF: population of %s descs failed %d\n",
			"images", rc);
		return rc;
	}

	/* update parent's authentication data */
	for (unsigned int i = 0U; i < MAX_NUMBER_IDS; i++) {
		if (auth_img_descs[i].parent != NULL) {
			rc = get_auth_param_type_desc(i,
						&type_desc,
						&auth_buf_size);
			if (rc < 0) {
				ERROR("FCONF: failed to get auth data %d\n",
					rc);
				return rc;
			}

			rc = update_parent_auth_data(auth_img_descs[i].parent,
						type_desc,
						auth_buf_size);
			if (rc < 0) {
				ERROR("FCONF: auth data update failed %d\n",
					rc);
				return rc;
			}
		}
	}

	return rc;
}

FCONF_REGISTER_POPULATOR(TB_FW, cot_desc, fconf_populate_cot_descs);
REGISTER_COT(cot_desc);