blob: 1b8ff82bcad33d0d2a1cf51db7367a49858d9190 [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Roberto Vargasbe126ed2018-02-12 12:36:17 +00002 * Copyright (c) 2015-2018, 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>
19#include <plat/common/platform.h>
20
Juan Castillobfb7fa62016-01-22 11:05:57 +000021/* ASN.1 tags */
22#define ASN1_INTEGER 0x02
23
Juan Castillo8e55d932015-04-02 09:48:16 +010024#define return_if_error(rc) \
25 do { \
26 if (rc != 0) { \
27 return rc; \
28 } \
29 } while (0)
30
dp-armb3e85802016-12-12 14:48:13 +000031#pragma weak plat_set_nv_ctr2
32
Juan Castillo8e55d932015-04-02 09:48:16 +010033/* Pointer to CoT */
34extern const auth_img_desc_t *const cot_desc_ptr;
Roberto Vargasbe126ed2018-02-12 12:36:17 +000035extern unsigned int auth_img_flags[MAX_NUMBER_IDS];
Juan Castillo8e55d932015-04-02 09:48:16 +010036
37static 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 */
50static 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 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
57 if (0 == cmp_auth_param_type_desc(param_type_desc,
58 img_desc->authenticated_data[i].type_desc)) {
59 *param = img_desc->authenticated_data[i].data.ptr;
60 *len = img_desc->authenticated_data[i].data.len;
61 return 0;
62 }
63 }
64
65 return 1;
66}
67
68/*
69 * Authenticate an image by matching the data hash
70 *
71 * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
72 * this method, the image must contain:
73 *
74 * - The data to calculate the hash from
75 *
76 * The parent image must contain:
77 *
78 * - The hash to be matched with (including hash algorithm)
79 *
80 * For a successful authentication, both hashes must match. The function calls
81 * the crypto-module to check this matching.
82 *
83 * Parameters:
84 * param: parameters to perform the hash authentication
85 * img_desc: pointer to image descriptor so we can know the image type
86 * and parent image
87 * img: pointer to image in memory
88 * img_len: length of image (in bytes)
89 *
90 * Return:
91 * 0 = success, Otherwise = error
92 */
93static int auth_hash(const auth_method_param_hash_t *param,
94 const auth_img_desc_t *img_desc,
95 void *img, unsigned int img_len)
96{
97 void *data_ptr, *hash_der_ptr;
98 unsigned int data_len, hash_der_len;
99 int rc = 0;
100
101 /* Get the hash from the parent image. This hash will be DER encoded
102 * and contain the hash algorithm */
103 rc = auth_get_param(param->hash, img_desc->parent,
104 &hash_der_ptr, &hash_der_len);
105 return_if_error(rc);
106
107 /* Get the data to be hashed from the current image */
108 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
109 img, img_len, &data_ptr, &data_len);
110 return_if_error(rc);
111
112 /* Ask the crypto module to verify this hash */
113 rc = crypto_mod_verify_hash(data_ptr, data_len,
114 hash_der_ptr, hash_der_len);
115
116 return rc;
117}
118
119/*
120 * Authenticate by digital signature
121 *
122 * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
123 * this method, the image must contain:
124 *
125 * - Data to be signed
126 * - Signature
127 * - Signature algorithm
128 *
129 * We rely on the image parser module to extract this data from the image.
130 * The parent image must contain:
131 *
132 * - Public key (or a hash of it)
133 *
134 * If the parent image contains only a hash of the key, we will try to obtain
135 * the public key from the image itself (i.e. self-signed certificates). In that
136 * case, the signature verification is considered just an integrity check and
137 * the authentication is established by calculating the hash of the key and
138 * comparing it with the hash obtained from the parent.
139 *
140 * If the image has no parent (NULL), it means it has to be authenticated using
141 * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
142 * or a hash of it.
143 *
144 * Return: 0 = success, Otherwise = error
145 */
146static int auth_signature(const auth_method_param_sig_t *param,
147 const auth_img_desc_t *img_desc,
148 void *img, unsigned int img_len)
149{
150 void *data_ptr, *pk_ptr, *pk_hash_ptr, *sig_ptr, *sig_alg_ptr;
151 unsigned int data_len, pk_len, pk_hash_len, sig_len, sig_alg_len;
152 unsigned int flags = 0;
153 int rc = 0;
154
155 /* Get the data to be signed from current image */
156 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
157 img, img_len, &data_ptr, &data_len);
158 return_if_error(rc);
159
160 /* Get the signature from current image */
161 rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
162 img, img_len, &sig_ptr, &sig_len);
163 return_if_error(rc);
164
165 /* Get the signature algorithm from current image */
166 rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
167 img, img_len, &sig_alg_ptr, &sig_alg_len);
168 return_if_error(rc);
169
170 /* Get the public key from the parent. If there is no parent (NULL),
171 * the certificate has been signed with the ROTPK, so we have to get
172 * the PK from the platform */
173 if (img_desc->parent) {
174 rc = auth_get_param(param->pk, img_desc->parent,
175 &pk_ptr, &pk_len);
176 } else {
177 rc = plat_get_rotpk_info(param->pk->cookie, &pk_ptr, &pk_len,
178 &flags);
179 }
180 return_if_error(rc);
181
Soby Mathew2f7ed052016-05-24 15:05:15 +0100182 if (flags & (ROTPK_IS_HASH | ROTPK_NOT_DEPLOYED)) {
183 /* If the PK is a hash of the key or if the ROTPK is not
184 deployed on the platform, retrieve the key from the image */
Juan Castillo8e55d932015-04-02 09:48:16 +0100185 pk_hash_ptr = pk_ptr;
186 pk_hash_len = pk_len;
187 rc = img_parser_get_auth_param(img_desc->img_type,
188 param->pk, img, img_len,
189 &pk_ptr, &pk_len);
190 return_if_error(rc);
191
192 /* Ask the crypto module to verify the signature */
193 rc = crypto_mod_verify_signature(data_ptr, data_len,
194 sig_ptr, sig_len,
195 sig_alg_ptr, sig_alg_len,
196 pk_ptr, pk_len);
197 return_if_error(rc);
198
Soby Mathew2f7ed052016-05-24 15:05:15 +0100199 if (flags & ROTPK_NOT_DEPLOYED) {
200 NOTICE("ROTPK is not deployed on platform. "
201 "Skipping ROTPK verification.\n");
202 } else {
203 /* Ask the crypto-module to verify the key hash */
204 rc = crypto_mod_verify_hash(pk_ptr, pk_len,
205 pk_hash_ptr, pk_hash_len);
206 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100207 } else {
208 /* Ask the crypto module to verify the signature */
209 rc = crypto_mod_verify_signature(data_ptr, data_len,
210 sig_ptr, sig_len,
211 sig_alg_ptr, sig_alg_len,
212 pk_ptr, pk_len);
213 }
214
215 return rc;
216}
217
218/*
Juan Castillobfb7fa62016-01-22 11:05:57 +0000219 * Authenticate by Non-Volatile counter
220 *
221 * To protect the system against rollback, the platform includes a non-volatile
222 * counter whose value can only be increased. All certificates include a counter
223 * value that should not be lower than the value stored in the platform. If the
224 * value is larger, the counter in the platform must be updated to the new
225 * value.
226 *
227 * Return: 0 = success, Otherwise = error
228 */
229static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
230 const auth_img_desc_t *img_desc,
231 void *img, unsigned int img_len)
232{
233 char *p;
234 void *data_ptr = NULL;
235 unsigned int data_len, len, i;
236 unsigned int cert_nv_ctr, plat_nv_ctr;
237 int rc = 0;
238
239 /* Get the counter value from current image. The AM expects the IPM
240 * to return the counter value as a DER encoded integer */
241 rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
242 img, img_len, &data_ptr, &data_len);
243 return_if_error(rc);
244
245 /* Parse the DER encoded integer */
246 assert(data_ptr);
247 p = (char *)data_ptr;
248 if (*p != ASN1_INTEGER) {
249 /* Invalid ASN.1 integer */
250 return 1;
251 }
252 p++;
253
254 /* NV-counters are unsigned integers up to 32-bit */
255 len = (unsigned int)(*p & 0x7f);
256 if ((*p & 0x80) || (len > 4)) {
257 return 1;
258 }
259 p++;
260
261 /* Check the number is not negative */
262 if (*p & 0x80) {
263 return 1;
264 }
265
266 /* Convert to unsigned int. This code is for a little-endian CPU */
267 cert_nv_ctr = 0;
268 for (i = 0; i < len; i++) {
269 cert_nv_ctr = (cert_nv_ctr << 8) | *p++;
270 }
271
272 /* Get the counter from the platform */
273 rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
274 return_if_error(rc);
275
276 if (cert_nv_ctr < plat_nv_ctr) {
277 /* Invalid NV-counter */
278 return 1;
279 } else if (cert_nv_ctr > plat_nv_ctr) {
dp-armb3e85802016-12-12 14:48:13 +0000280 rc = plat_set_nv_ctr2(param->plat_nv_ctr->cookie,
281 img_desc, cert_nv_ctr);
282 return_if_error(rc);
Juan Castillobfb7fa62016-01-22 11:05:57 +0000283 }
284
285 return 0;
286}
287
dp-armb3e85802016-12-12 14:48:13 +0000288int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
289 unsigned int nv_ctr)
290{
291 return plat_set_nv_ctr(cookie, nv_ctr);
292}
293
Juan Castillobfb7fa62016-01-22 11:05:57 +0000294/*
Juan Castillo8e55d932015-04-02 09:48:16 +0100295 * Return the parent id in the output parameter '*parent_id'
296 *
297 * Return value:
298 * 0 = Image has parent, 1 = Image has no parent or parent is authenticated
299 */
300int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
301{
302 const auth_img_desc_t *img_desc = NULL;
303
304 assert(parent_id != NULL);
305
306 /* Get the image descriptor */
307 img_desc = &cot_desc_ptr[img_id];
308
309 /* Check if the image has no parent (ROT) */
310 if (img_desc->parent == NULL) {
311 *parent_id = 0;
312 return 1;
313 }
314
315 /* Check if the parent has already been authenticated */
316 if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
317 *parent_id = 0;
318 return 1;
319 }
320
321 *parent_id = img_desc->parent->img_id;
322 return 0;
323}
324
325/*
326 * Initialize the different modules in the authentication framework
327 */
328void auth_mod_init(void)
329{
330 /* Check we have a valid CoT registered */
331 assert(cot_desc_ptr != NULL);
332
333 /* Crypto module */
334 crypto_mod_init();
335
336 /* Image parser module */
337 img_parser_init();
338}
339
340/*
341 * Authenticate a certificate/image
342 *
343 * Return: 0 = success, Otherwise = error
344 */
345int auth_mod_verify_img(unsigned int img_id,
346 void *img_ptr,
347 unsigned int img_len)
348{
349 const auth_img_desc_t *img_desc = NULL;
350 const auth_method_desc_t *auth_method = NULL;
351 void *param_ptr;
352 unsigned int param_len;
353 int rc, i;
354
355 /* Get the image descriptor from the chain of trust */
356 img_desc = &cot_desc_ptr[img_id];
357
358 /* Ask the parser to check the image integrity */
359 rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
360 return_if_error(rc);
361
362 /* Authenticate the image using the methods indicated in the image
363 * descriptor. */
364 for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
365 auth_method = &img_desc->img_auth_methods[i];
366 switch (auth_method->type) {
367 case AUTH_METHOD_NONE:
368 rc = 0;
369 break;
370 case AUTH_METHOD_HASH:
371 rc = auth_hash(&auth_method->param.hash,
372 img_desc, img_ptr, img_len);
373 break;
374 case AUTH_METHOD_SIG:
375 rc = auth_signature(&auth_method->param.sig,
376 img_desc, img_ptr, img_len);
377 break;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000378 case AUTH_METHOD_NV_CTR:
379 rc = auth_nvctr(&auth_method->param.nv_ctr,
380 img_desc, img_ptr, img_len);
381 break;
Juan Castillo8e55d932015-04-02 09:48:16 +0100382 default:
383 /* Unknown authentication method */
384 rc = 1;
385 break;
386 }
387 return_if_error(rc);
388 }
389
390 /* Extract the parameters indicated in the image descriptor to
391 * authenticate the children images. */
392 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
393 if (img_desc->authenticated_data[i].type_desc == NULL) {
394 continue;
395 }
396
397 /* Get the parameter from the image parser module */
398 rc = img_parser_get_auth_param(img_desc->img_type,
399 img_desc->authenticated_data[i].type_desc,
400 img_ptr, img_len, &param_ptr, &param_len);
401 return_if_error(rc);
402
403 /* Check parameter size */
404 if (param_len > img_desc->authenticated_data[i].data.len) {
405 return 1;
406 }
407
408 /* Copy the parameter for later use */
409 memcpy((void *)img_desc->authenticated_data[i].data.ptr,
410 (void *)param_ptr, param_len);
411 }
412
413 /* Mark image as authenticated */
414 auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
415
416 return 0;
417}