blob: bdd3c5a1023108ef389f17f6a686fd83d97073f8 [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
43#define return_if_error(rc) \
44 do { \
45 if (rc != 0) { \
46 return rc; \
47 } \
48 } while (0)
49
50/* Pointer to CoT */
51extern const auth_img_desc_t *const cot_desc_ptr;
52extern unsigned int auth_img_flags[];
53
54static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
55 const auth_param_type_desc_t *b)
56{
57 if ((a->type == b->type) && (a->cookie == b->cookie)) {
58 return 0;
59 }
60 return 1;
61}
62
63/*
64 * This function obtains the requested authentication parameter data from the
65 * information extracted from the parent image after its authentication.
66 */
67static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
68 const auth_img_desc_t *img_desc,
69 void **param, unsigned int *len)
70{
71 int i;
72
73 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
74 if (0 == cmp_auth_param_type_desc(param_type_desc,
75 img_desc->authenticated_data[i].type_desc)) {
76 *param = img_desc->authenticated_data[i].data.ptr;
77 *len = img_desc->authenticated_data[i].data.len;
78 return 0;
79 }
80 }
81
82 return 1;
83}
84
85/*
86 * Authenticate an image by matching the data hash
87 *
88 * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
89 * this method, the image must contain:
90 *
91 * - The data to calculate the hash from
92 *
93 * The parent image must contain:
94 *
95 * - The hash to be matched with (including hash algorithm)
96 *
97 * For a successful authentication, both hashes must match. The function calls
98 * the crypto-module to check this matching.
99 *
100 * Parameters:
101 * param: parameters to perform the hash authentication
102 * img_desc: pointer to image descriptor so we can know the image type
103 * and parent image
104 * img: pointer to image in memory
105 * img_len: length of image (in bytes)
106 *
107 * Return:
108 * 0 = success, Otherwise = error
109 */
110static int auth_hash(const auth_method_param_hash_t *param,
111 const auth_img_desc_t *img_desc,
112 void *img, unsigned int img_len)
113{
114 void *data_ptr, *hash_der_ptr;
115 unsigned int data_len, hash_der_len;
116 int rc = 0;
117
118 /* Get the hash from the parent image. This hash will be DER encoded
119 * and contain the hash algorithm */
120 rc = auth_get_param(param->hash, img_desc->parent,
121 &hash_der_ptr, &hash_der_len);
122 return_if_error(rc);
123
124 /* Get the data to be hashed from the current image */
125 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
126 img, img_len, &data_ptr, &data_len);
127 return_if_error(rc);
128
129 /* Ask the crypto module to verify this hash */
130 rc = crypto_mod_verify_hash(data_ptr, data_len,
131 hash_der_ptr, hash_der_len);
132
133 return rc;
134}
135
136/*
137 * Authenticate by digital signature
138 *
139 * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
140 * this method, the image must contain:
141 *
142 * - Data to be signed
143 * - Signature
144 * - Signature algorithm
145 *
146 * We rely on the image parser module to extract this data from the image.
147 * The parent image must contain:
148 *
149 * - Public key (or a hash of it)
150 *
151 * If the parent image contains only a hash of the key, we will try to obtain
152 * the public key from the image itself (i.e. self-signed certificates). In that
153 * case, the signature verification is considered just an integrity check and
154 * the authentication is established by calculating the hash of the key and
155 * comparing it with the hash obtained from the parent.
156 *
157 * If the image has no parent (NULL), it means it has to be authenticated using
158 * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
159 * or a hash of it.
160 *
161 * Return: 0 = success, Otherwise = error
162 */
163static int auth_signature(const auth_method_param_sig_t *param,
164 const auth_img_desc_t *img_desc,
165 void *img, unsigned int img_len)
166{
167 void *data_ptr, *pk_ptr, *pk_hash_ptr, *sig_ptr, *sig_alg_ptr;
168 unsigned int data_len, pk_len, pk_hash_len, sig_len, sig_alg_len;
169 unsigned int flags = 0;
170 int rc = 0;
171
172 /* Get the data to be signed from current image */
173 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
174 img, img_len, &data_ptr, &data_len);
175 return_if_error(rc);
176
177 /* Get the signature from current image */
178 rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
179 img, img_len, &sig_ptr, &sig_len);
180 return_if_error(rc);
181
182 /* Get the signature algorithm from current image */
183 rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
184 img, img_len, &sig_alg_ptr, &sig_alg_len);
185 return_if_error(rc);
186
187 /* Get the public key from the parent. If there is no parent (NULL),
188 * the certificate has been signed with the ROTPK, so we have to get
189 * the PK from the platform */
190 if (img_desc->parent) {
191 rc = auth_get_param(param->pk, img_desc->parent,
192 &pk_ptr, &pk_len);
193 } else {
194 rc = plat_get_rotpk_info(param->pk->cookie, &pk_ptr, &pk_len,
195 &flags);
196 }
197 return_if_error(rc);
198
199 /* If the PK is a hash of the key, retrieve the key from the image */
200 if (flags & ROTPK_IS_HASH) {
201 pk_hash_ptr = pk_ptr;
202 pk_hash_len = pk_len;
203 rc = img_parser_get_auth_param(img_desc->img_type,
204 param->pk, img, img_len,
205 &pk_ptr, &pk_len);
206 return_if_error(rc);
207
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 return_if_error(rc);
214
215 /* Ask the crypto-module to verify the key hash */
216 rc = crypto_mod_verify_hash(pk_ptr, pk_len,
217 pk_hash_ptr, pk_hash_len);
218 } else {
219 /* Ask the crypto module to verify the signature */
220 rc = crypto_mod_verify_signature(data_ptr, data_len,
221 sig_ptr, sig_len,
222 sig_alg_ptr, sig_alg_len,
223 pk_ptr, pk_len);
224 }
225
226 return rc;
227}
228
229/*
230 * Return the parent id in the output parameter '*parent_id'
231 *
232 * Return value:
233 * 0 = Image has parent, 1 = Image has no parent or parent is authenticated
234 */
235int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
236{
237 const auth_img_desc_t *img_desc = NULL;
238
239 assert(parent_id != NULL);
240
241 /* Get the image descriptor */
242 img_desc = &cot_desc_ptr[img_id];
243
244 /* Check if the image has no parent (ROT) */
245 if (img_desc->parent == NULL) {
246 *parent_id = 0;
247 return 1;
248 }
249
250 /* Check if the parent has already been authenticated */
251 if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
252 *parent_id = 0;
253 return 1;
254 }
255
256 *parent_id = img_desc->parent->img_id;
257 return 0;
258}
259
260/*
261 * Initialize the different modules in the authentication framework
262 */
263void auth_mod_init(void)
264{
265 /* Check we have a valid CoT registered */
266 assert(cot_desc_ptr != NULL);
267
268 /* Crypto module */
269 crypto_mod_init();
270
271 /* Image parser module */
272 img_parser_init();
273}
274
275/*
276 * Authenticate a certificate/image
277 *
278 * Return: 0 = success, Otherwise = error
279 */
280int auth_mod_verify_img(unsigned int img_id,
281 void *img_ptr,
282 unsigned int img_len)
283{
284 const auth_img_desc_t *img_desc = NULL;
285 const auth_method_desc_t *auth_method = NULL;
286 void *param_ptr;
287 unsigned int param_len;
288 int rc, i;
289
290 /* Get the image descriptor from the chain of trust */
291 img_desc = &cot_desc_ptr[img_id];
292
293 /* Ask the parser to check the image integrity */
294 rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
295 return_if_error(rc);
296
297 /* Authenticate the image using the methods indicated in the image
298 * descriptor. */
299 for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
300 auth_method = &img_desc->img_auth_methods[i];
301 switch (auth_method->type) {
302 case AUTH_METHOD_NONE:
303 rc = 0;
304 break;
305 case AUTH_METHOD_HASH:
306 rc = auth_hash(&auth_method->param.hash,
307 img_desc, img_ptr, img_len);
308 break;
309 case AUTH_METHOD_SIG:
310 rc = auth_signature(&auth_method->param.sig,
311 img_desc, img_ptr, img_len);
312 break;
313 default:
314 /* Unknown authentication method */
315 rc = 1;
316 break;
317 }
318 return_if_error(rc);
319 }
320
321 /* Extract the parameters indicated in the image descriptor to
322 * authenticate the children images. */
323 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
324 if (img_desc->authenticated_data[i].type_desc == NULL) {
325 continue;
326 }
327
328 /* Get the parameter from the image parser module */
329 rc = img_parser_get_auth_param(img_desc->img_type,
330 img_desc->authenticated_data[i].type_desc,
331 img_ptr, img_len, &param_ptr, &param_len);
332 return_if_error(rc);
333
334 /* Check parameter size */
335 if (param_len > img_desc->authenticated_data[i].data.len) {
336 return 1;
337 }
338
339 /* Copy the parameter for later use */
340 memcpy((void *)img_desc->authenticated_data[i].data.ptr,
341 (void *)param_ptr, param_len);
342 }
343
344 /* Mark image as authenticated */
345 auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
346
347 return 0;
348}