blob: 418455618dbf8b7cf52e2303b6f8dbbad3c29cc6 [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
202 /* If the PK is a hash of the key, retrieve the key from the image */
203 if (flags & ROTPK_IS_HASH) {
204 pk_hash_ptr = pk_ptr;
205 pk_hash_len = pk_len;
206 rc = img_parser_get_auth_param(img_desc->img_type,
207 param->pk, img, img_len,
208 &pk_ptr, &pk_len);
209 return_if_error(rc);
210
211 /* Ask the crypto module to verify the signature */
212 rc = crypto_mod_verify_signature(data_ptr, data_len,
213 sig_ptr, sig_len,
214 sig_alg_ptr, sig_alg_len,
215 pk_ptr, pk_len);
216 return_if_error(rc);
217
218 /* Ask the crypto-module to verify the key hash */
219 rc = crypto_mod_verify_hash(pk_ptr, pk_len,
220 pk_hash_ptr, pk_hash_len);
221 } else {
222 /* Ask the crypto module to verify the signature */
223 rc = crypto_mod_verify_signature(data_ptr, data_len,
224 sig_ptr, sig_len,
225 sig_alg_ptr, sig_alg_len,
226 pk_ptr, pk_len);
227 }
228
229 return rc;
230}
231
232/*
Juan Castillobfb7fa62016-01-22 11:05:57 +0000233 * Authenticate by Non-Volatile counter
234 *
235 * To protect the system against rollback, the platform includes a non-volatile
236 * counter whose value can only be increased. All certificates include a counter
237 * value that should not be lower than the value stored in the platform. If the
238 * value is larger, the counter in the platform must be updated to the new
239 * value.
240 *
241 * Return: 0 = success, Otherwise = error
242 */
243static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
244 const auth_img_desc_t *img_desc,
245 void *img, unsigned int img_len)
246{
247 char *p;
248 void *data_ptr = NULL;
249 unsigned int data_len, len, i;
250 unsigned int cert_nv_ctr, plat_nv_ctr;
251 int rc = 0;
252
253 /* Get the counter value from current image. The AM expects the IPM
254 * to return the counter value as a DER encoded integer */
255 rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
256 img, img_len, &data_ptr, &data_len);
257 return_if_error(rc);
258
259 /* Parse the DER encoded integer */
260 assert(data_ptr);
261 p = (char *)data_ptr;
262 if (*p != ASN1_INTEGER) {
263 /* Invalid ASN.1 integer */
264 return 1;
265 }
266 p++;
267
268 /* NV-counters are unsigned integers up to 32-bit */
269 len = (unsigned int)(*p & 0x7f);
270 if ((*p & 0x80) || (len > 4)) {
271 return 1;
272 }
273 p++;
274
275 /* Check the number is not negative */
276 if (*p & 0x80) {
277 return 1;
278 }
279
280 /* Convert to unsigned int. This code is for a little-endian CPU */
281 cert_nv_ctr = 0;
282 for (i = 0; i < len; i++) {
283 cert_nv_ctr = (cert_nv_ctr << 8) | *p++;
284 }
285
286 /* Get the counter from the platform */
287 rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
288 return_if_error(rc);
289
290 if (cert_nv_ctr < plat_nv_ctr) {
291 /* Invalid NV-counter */
292 return 1;
293 } else if (cert_nv_ctr > plat_nv_ctr) {
294 if (img_desc->parent == NULL) {
295 /* This certificate has been signed with the ROT key.
296 * Update the platform counter value */
297 rc = plat_set_nv_ctr(param->plat_nv_ctr->cookie,
298 cert_nv_ctr);
299 return_if_error(rc);
300 } else {
301 /* Secondary certificates cannot modify the counter */
302 return 1;
303 }
304 }
305
306 return 0;
307}
308
309/*
Juan Castillo8e55d932015-04-02 09:48:16 +0100310 * Return the parent id in the output parameter '*parent_id'
311 *
312 * Return value:
313 * 0 = Image has parent, 1 = Image has no parent or parent is authenticated
314 */
315int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
316{
317 const auth_img_desc_t *img_desc = NULL;
318
319 assert(parent_id != NULL);
320
321 /* Get the image descriptor */
322 img_desc = &cot_desc_ptr[img_id];
323
324 /* Check if the image has no parent (ROT) */
325 if (img_desc->parent == NULL) {
326 *parent_id = 0;
327 return 1;
328 }
329
330 /* Check if the parent has already been authenticated */
331 if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
332 *parent_id = 0;
333 return 1;
334 }
335
336 *parent_id = img_desc->parent->img_id;
337 return 0;
338}
339
340/*
341 * Initialize the different modules in the authentication framework
342 */
343void auth_mod_init(void)
344{
345 /* Check we have a valid CoT registered */
346 assert(cot_desc_ptr != NULL);
347
348 /* Crypto module */
349 crypto_mod_init();
350
351 /* Image parser module */
352 img_parser_init();
353}
354
355/*
356 * Authenticate a certificate/image
357 *
358 * Return: 0 = success, Otherwise = error
359 */
360int auth_mod_verify_img(unsigned int img_id,
361 void *img_ptr,
362 unsigned int img_len)
363{
364 const auth_img_desc_t *img_desc = NULL;
365 const auth_method_desc_t *auth_method = NULL;
366 void *param_ptr;
367 unsigned int param_len;
368 int rc, i;
369
370 /* Get the image descriptor from the chain of trust */
371 img_desc = &cot_desc_ptr[img_id];
372
373 /* Ask the parser to check the image integrity */
374 rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
375 return_if_error(rc);
376
377 /* Authenticate the image using the methods indicated in the image
378 * descriptor. */
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 break;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000393 case AUTH_METHOD_NV_CTR:
394 rc = auth_nvctr(&auth_method->param.nv_ctr,
395 img_desc, img_ptr, img_len);
396 break;
Juan Castillo8e55d932015-04-02 09:48:16 +0100397 default:
398 /* Unknown authentication method */
399 rc = 1;
400 break;
401 }
402 return_if_error(rc);
403 }
404
405 /* Extract the parameters indicated in the image descriptor to
406 * authenticate the children images. */
407 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
408 if (img_desc->authenticated_data[i].type_desc == NULL) {
409 continue;
410 }
411
412 /* Get the parameter from the image parser module */
413 rc = img_parser_get_auth_param(img_desc->img_type,
414 img_desc->authenticated_data[i].type_desc,
415 img_ptr, img_len, &param_ptr, &param_len);
416 return_if_error(rc);
417
418 /* Check parameter size */
419 if (param_len > img_desc->authenticated_data[i].data.len) {
420 return 1;
421 }
422
423 /* Copy the parameter for later use */
424 memcpy((void *)img_desc->authenticated_data[i].data.ptr,
425 (void *)param_ptr, param_len);
426 }
427
428 /* Mark image as authenticated */
429 auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
430
431 return 0;
432}