blob: c7f84afdf5aa733f59a7824e839e91a54540f3f7 [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Louis Mayencourt944ade82019-08-08 12:03:26 +01002 * Copyright (c) 2015-2020, 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>
Louis Mayencourt944ade82019-08-08 12:03:26 +010019#include <lib/fconf/fconf_tbbr_getter.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <plat/common/platform.h>
21
Juan Castillobfb7fa62016-01-22 11:05:57 +000022/* ASN.1 tags */
23#define ASN1_INTEGER 0x02
24
Juan Castillo8e55d932015-04-02 09:48:16 +010025#define return_if_error(rc) \
26 do { \
27 if (rc != 0) { \
28 return rc; \
29 } \
30 } while (0)
31
dp-armb3e85802016-12-12 14:48:13 +000032#pragma weak plat_set_nv_ctr2
33
Joel Huttone9919bb2019-02-20 11:56:46 +000034
Juan Castillo8e55d932015-04-02 09:48:16 +010035static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
36 const auth_param_type_desc_t *b)
37{
38 if ((a->type == b->type) && (a->cookie == b->cookie)) {
39 return 0;
40 }
41 return 1;
42}
43
44/*
45 * This function obtains the requested authentication parameter data from the
46 * information extracted from the parent image after its authentication.
47 */
48static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
49 const auth_img_desc_t *img_desc,
50 void **param, unsigned int *len)
51{
52 int i;
53
Joel Hutton69931af2019-03-11 11:37:38 +000054 if (img_desc->authenticated_data == NULL)
55 return 1;
56
Juan Castillo8e55d932015-04-02 09:48:16 +010057 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
58 if (0 == cmp_auth_param_type_desc(param_type_desc,
59 img_desc->authenticated_data[i].type_desc)) {
60 *param = img_desc->authenticated_data[i].data.ptr;
61 *len = img_desc->authenticated_data[i].data.len;
62 return 0;
63 }
64 }
65
66 return 1;
67}
68
69/*
70 * Authenticate an image by matching the data hash
71 *
72 * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
73 * this method, the image must contain:
74 *
75 * - The data to calculate the hash from
76 *
77 * The parent image must contain:
78 *
79 * - The hash to be matched with (including hash algorithm)
80 *
81 * For a successful authentication, both hashes must match. The function calls
82 * the crypto-module to check this matching.
83 *
84 * Parameters:
85 * param: parameters to perform the hash authentication
86 * img_desc: pointer to image descriptor so we can know the image type
87 * and parent image
88 * img: pointer to image in memory
89 * img_len: length of image (in bytes)
90 *
91 * Return:
92 * 0 = success, Otherwise = error
93 */
94static int auth_hash(const auth_method_param_hash_t *param,
95 const auth_img_desc_t *img_desc,
96 void *img, unsigned int img_len)
97{
98 void *data_ptr, *hash_der_ptr;
99 unsigned int data_len, hash_der_len;
100 int rc = 0;
101
102 /* Get the hash from the parent image. This hash will be DER encoded
103 * and contain the hash algorithm */
104 rc = auth_get_param(param->hash, img_desc->parent,
105 &hash_der_ptr, &hash_der_len);
106 return_if_error(rc);
107
108 /* Get the data to be hashed from the current image */
109 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
110 img, img_len, &data_ptr, &data_len);
111 return_if_error(rc);
112
113 /* Ask the crypto module to verify this hash */
114 rc = crypto_mod_verify_hash(data_ptr, data_len,
115 hash_der_ptr, hash_der_len);
116
117 return rc;
118}
119
120/*
121 * Authenticate by digital signature
122 *
123 * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
124 * this method, the image must contain:
125 *
126 * - Data to be signed
127 * - Signature
128 * - Signature algorithm
129 *
130 * We rely on the image parser module to extract this data from the image.
131 * The parent image must contain:
132 *
133 * - Public key (or a hash of it)
134 *
135 * If the parent image contains only a hash of the key, we will try to obtain
136 * the public key from the image itself (i.e. self-signed certificates). In that
137 * case, the signature verification is considered just an integrity check and
138 * the authentication is established by calculating the hash of the key and
139 * comparing it with the hash obtained from the parent.
140 *
141 * If the image has no parent (NULL), it means it has to be authenticated using
142 * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
143 * or a hash of it.
144 *
145 * Return: 0 = success, Otherwise = error
146 */
147static int auth_signature(const auth_method_param_sig_t *param,
148 const auth_img_desc_t *img_desc,
149 void *img, unsigned int img_len)
150{
151 void *data_ptr, *pk_ptr, *pk_hash_ptr, *sig_ptr, *sig_alg_ptr;
152 unsigned int data_len, pk_len, pk_hash_len, sig_len, sig_alg_len;
153 unsigned int flags = 0;
154 int rc = 0;
155
156 /* Get the data to be signed from current image */
157 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
158 img, img_len, &data_ptr, &data_len);
159 return_if_error(rc);
160
161 /* Get the signature from current image */
162 rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
163 img, img_len, &sig_ptr, &sig_len);
164 return_if_error(rc);
165
166 /* Get the signature algorithm from current image */
167 rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
168 img, img_len, &sig_alg_ptr, &sig_alg_len);
169 return_if_error(rc);
170
171 /* Get the public key from the parent. If there is no parent (NULL),
172 * the certificate has been signed with the ROTPK, so we have to get
173 * the PK from the platform */
174 if (img_desc->parent) {
175 rc = auth_get_param(param->pk, img_desc->parent,
176 &pk_ptr, &pk_len);
177 } else {
178 rc = plat_get_rotpk_info(param->pk->cookie, &pk_ptr, &pk_len,
179 &flags);
180 }
181 return_if_error(rc);
182
Soby Mathew2f7ed052016-05-24 15:05:15 +0100183 if (flags & (ROTPK_IS_HASH | ROTPK_NOT_DEPLOYED)) {
184 /* If the PK is a hash of the key or if the ROTPK is not
185 deployed on the platform, retrieve the key from the image */
Juan Castillo8e55d932015-04-02 09:48:16 +0100186 pk_hash_ptr = pk_ptr;
187 pk_hash_len = pk_len;
188 rc = img_parser_get_auth_param(img_desc->img_type,
189 param->pk, img, img_len,
190 &pk_ptr, &pk_len);
191 return_if_error(rc);
192
193 /* Ask the crypto module to verify the signature */
194 rc = crypto_mod_verify_signature(data_ptr, data_len,
195 sig_ptr, sig_len,
196 sig_alg_ptr, sig_alg_len,
197 pk_ptr, pk_len);
198 return_if_error(rc);
199
Soby Mathew2f7ed052016-05-24 15:05:15 +0100200 if (flags & ROTPK_NOT_DEPLOYED) {
201 NOTICE("ROTPK is not deployed on platform. "
202 "Skipping ROTPK verification.\n");
203 } else {
204 /* Ask the crypto-module to verify the key hash */
205 rc = crypto_mod_verify_hash(pk_ptr, pk_len,
206 pk_hash_ptr, pk_hash_len);
207 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100208 } else {
209 /* Ask the crypto module to verify the signature */
210 rc = crypto_mod_verify_signature(data_ptr, data_len,
211 sig_ptr, sig_len,
212 sig_alg_ptr, sig_alg_len,
213 pk_ptr, pk_len);
214 }
215
216 return rc;
217}
218
219/*
Juan Castillobfb7fa62016-01-22 11:05:57 +0000220 * Authenticate by Non-Volatile counter
221 *
222 * To protect the system against rollback, the platform includes a non-volatile
223 * counter whose value can only be increased. All certificates include a counter
224 * value that should not be lower than the value stored in the platform. If the
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100225 * value is larger, the counter in the platform must be updated to the new value
226 * (provided it has been authenticated).
Juan Castillobfb7fa62016-01-22 11:05:57 +0000227 *
228 * Return: 0 = success, Otherwise = error
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100229 * Returns additionally,
230 * cert_nv_ctr -> NV counter value present in the certificate
231 * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed
232 * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed
Juan Castillobfb7fa62016-01-22 11:05:57 +0000233 */
234static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
235 const auth_img_desc_t *img_desc,
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100236 void *img, unsigned int img_len,
237 unsigned int *cert_nv_ctr,
238 bool *need_nv_ctr_upgrade)
Juan Castillobfb7fa62016-01-22 11:05:57 +0000239{
240 char *p;
241 void *data_ptr = NULL;
242 unsigned int data_len, len, i;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100243 unsigned int plat_nv_ctr;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000244 int rc = 0;
245
246 /* Get the counter value from current image. The AM expects the IPM
247 * to return the counter value as a DER encoded integer */
248 rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
249 img, img_len, &data_ptr, &data_len);
250 return_if_error(rc);
251
252 /* Parse the DER encoded integer */
253 assert(data_ptr);
254 p = (char *)data_ptr;
255 if (*p != ASN1_INTEGER) {
256 /* Invalid ASN.1 integer */
257 return 1;
258 }
259 p++;
260
261 /* NV-counters are unsigned integers up to 32-bit */
262 len = (unsigned int)(*p & 0x7f);
263 if ((*p & 0x80) || (len > 4)) {
264 return 1;
265 }
266 p++;
267
268 /* Check the number is not negative */
269 if (*p & 0x80) {
270 return 1;
271 }
272
273 /* Convert to unsigned int. This code is for a little-endian CPU */
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100274 *cert_nv_ctr = 0;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000275 for (i = 0; i < len; i++) {
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100276 *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000277 }
278
279 /* Get the counter from the platform */
280 rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
281 return_if_error(rc);
282
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100283 if (*cert_nv_ctr < plat_nv_ctr) {
Juan Castillobfb7fa62016-01-22 11:05:57 +0000284 /* Invalid NV-counter */
285 return 1;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100286 } else if (*cert_nv_ctr > plat_nv_ctr) {
287 *need_nv_ctr_upgrade = true;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000288 }
289
290 return 0;
291}
292
dp-armb3e85802016-12-12 14:48:13 +0000293int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
294 unsigned int nv_ctr)
295{
296 return plat_set_nv_ctr(cookie, nv_ctr);
297}
298
Juan Castillobfb7fa62016-01-22 11:05:57 +0000299/*
Juan Castillo8e55d932015-04-02 09:48:16 +0100300 * Return the parent id in the output parameter '*parent_id'
301 *
302 * Return value:
303 * 0 = Image has parent, 1 = Image has no parent or parent is authenticated
304 */
305int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
306{
307 const auth_img_desc_t *img_desc = NULL;
308
309 assert(parent_id != NULL);
Juan Castillo8e55d932015-04-02 09:48:16 +0100310 /* Get the image descriptor */
Louis Mayencourt944ade82019-08-08 12:03:26 +0100311 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
Juan Castillo8e55d932015-04-02 09:48:16 +0100312
313 /* Check if the image has no parent (ROT) */
314 if (img_desc->parent == NULL) {
315 *parent_id = 0;
316 return 1;
317 }
318
319 /* Check if the parent has already been authenticated */
320 if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
321 *parent_id = 0;
322 return 1;
323 }
324
325 *parent_id = img_desc->parent->img_id;
326 return 0;
327}
328
329/*
330 * Initialize the different modules in the authentication framework
331 */
332void auth_mod_init(void)
333{
334 /* Check we have a valid CoT registered */
335 assert(cot_desc_ptr != NULL);
336
337 /* Crypto module */
338 crypto_mod_init();
339
340 /* Image parser module */
341 img_parser_init();
342}
343
344/*
345 * Authenticate a certificate/image
346 *
347 * Return: 0 = success, Otherwise = error
348 */
349int auth_mod_verify_img(unsigned int img_id,
350 void *img_ptr,
351 unsigned int img_len)
352{
353 const auth_img_desc_t *img_desc = NULL;
354 const auth_method_desc_t *auth_method = NULL;
355 void *param_ptr;
356 unsigned int param_len;
357 int rc, i;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100358 unsigned int cert_nv_ctr = 0;
359 bool need_nv_ctr_upgrade = false;
360 bool sig_auth_done = false;
361 const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
Juan Castillo8e55d932015-04-02 09:48:16 +0100362
363 /* Get the image descriptor from the chain of trust */
Louis Mayencourt944ade82019-08-08 12:03:26 +0100364 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
Juan Castillo8e55d932015-04-02 09:48:16 +0100365
366 /* Ask the parser to check the image integrity */
367 rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
368 return_if_error(rc);
369
370 /* Authenticate the image using the methods indicated in the image
371 * descriptor. */
Joel Hutton8634cff2019-04-09 09:25:55 +0100372 if (img_desc->img_auth_methods == NULL)
Joel Hutton69931af2019-03-11 11:37:38 +0000373 return 1;
Juan Castillo8e55d932015-04-02 09:48:16 +0100374 for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
375 auth_method = &img_desc->img_auth_methods[i];
376 switch (auth_method->type) {
377 case AUTH_METHOD_NONE:
378 rc = 0;
379 break;
380 case AUTH_METHOD_HASH:
381 rc = auth_hash(&auth_method->param.hash,
382 img_desc, img_ptr, img_len);
383 break;
384 case AUTH_METHOD_SIG:
385 rc = auth_signature(&auth_method->param.sig,
386 img_desc, img_ptr, img_len);
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100387 sig_auth_done = true;
Juan Castillo8e55d932015-04-02 09:48:16 +0100388 break;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000389 case AUTH_METHOD_NV_CTR:
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100390 nv_ctr_param = &auth_method->param.nv_ctr;
391 rc = auth_nvctr(nv_ctr_param,
392 img_desc, img_ptr, img_len,
393 &cert_nv_ctr, &need_nv_ctr_upgrade);
Juan Castillobfb7fa62016-01-22 11:05:57 +0000394 break;
Juan Castillo8e55d932015-04-02 09:48:16 +0100395 default:
396 /* Unknown authentication method */
397 rc = 1;
398 break;
399 }
400 return_if_error(rc);
401 }
402
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100403 /*
404 * Do platform NV counter upgrade only if the certificate gets
405 * authenticated, and platform NV-counter upgrade is needed.
406 */
407 if (need_nv_ctr_upgrade && sig_auth_done) {
408 rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
409 img_desc, cert_nv_ctr);
410 return_if_error(rc);
411 }
412
Juan Castillo8e55d932015-04-02 09:48:16 +0100413 /* Extract the parameters indicated in the image descriptor to
414 * authenticate the children images. */
Joel Hutton69931af2019-03-11 11:37:38 +0000415 if (img_desc->authenticated_data != NULL) {
416 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
417 if (img_desc->authenticated_data[i].type_desc == NULL) {
418 continue;
419 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100420
Joel Hutton69931af2019-03-11 11:37:38 +0000421 /* Get the parameter from the image parser module */
422 rc = img_parser_get_auth_param(img_desc->img_type,
423 img_desc->authenticated_data[i].type_desc,
424 img_ptr, img_len, &param_ptr, &param_len);
425 return_if_error(rc);
Juan Castillo8e55d932015-04-02 09:48:16 +0100426
Joel Hutton69931af2019-03-11 11:37:38 +0000427 /* Check parameter size */
428 if (param_len > img_desc->authenticated_data[i].data.len) {
429 return 1;
430 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100431
Joel Hutton69931af2019-03-11 11:37:38 +0000432 /* Copy the parameter for later use */
433 memcpy((void *)img_desc->authenticated_data[i].data.ptr,
434 (void *)param_ptr, param_len);
435 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100436 }
437
438 /* Mark image as authenticated */
439 auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
440
441 return 0;
442}