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