blob: 8bde5bb7ccabf70f2c32e24e886da21a089a5d7f [file] [log] [blame]
Juan Castilloa57a4d52015-04-02 15:44:20 +01001/*
Yann Gautierbb7ffdf2023-01-06 11:47:26 +01002 * Copyright (c) 2015-2023, ARM Limited and Contributors. All rights reserved.
Juan Castilloa57a4d52015-04-02 15:44:20 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castilloa57a4d52015-04-02 15:44:20 +01005 */
6
7/*
Juan Castillobae6b2a2015-11-05 09:24:53 +00008 * X509 parser based on mbed TLS
Juan Castilloa57a4d52015-04-02 15:44:20 +01009 *
10 * This module implements functions to check the integrity of a X509v3
11 * certificate ASN.1 structure and extract authentication parameters from the
12 * extensions field, such as an image hash or a public key.
13 */
14
15#include <assert.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010016#include <stddef.h>
17#include <stdint.h>
18#include <string.h>
19
Juan Castillobae6b2a2015-11-05 09:24:53 +000020/* mbed TLS headers */
21#include <mbedtls/asn1.h>
22#include <mbedtls/oid.h>
23#include <mbedtls/platform.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010024
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000025#include <arch_helpers.h>
26#include <drivers/auth/img_parser_mod.h>
27#include <drivers/auth/mbedtls/mbedtls_common.h>
28#include <lib/utils.h>
29
Juan Castilloa57a4d52015-04-02 15:44:20 +010030/* Maximum OID string length ("a.b.c.d.e.f ...") */
31#define MAX_OID_STR_LEN 64
32
Juan Castillobae6b2a2015-11-05 09:24:53 +000033#define LIB_NAME "mbed TLS X509v3"
Juan Castilloa57a4d52015-04-02 15:44:20 +010034
35/* Temporary variables to speed up the authentication parameters search. These
36 * variables are assigned once during the integrity check and used any time an
37 * authentication parameter is requested, so we do not have to parse the image
38 * again */
Juan Castillobae6b2a2015-11-05 09:24:53 +000039static mbedtls_asn1_buf tbs;
40static mbedtls_asn1_buf v3_ext;
41static mbedtls_asn1_buf pk;
42static mbedtls_asn1_buf sig_alg;
43static mbedtls_asn1_buf signature;
Juan Castilloa57a4d52015-04-02 15:44:20 +010044
45/*
Antonio Nino Diaz30eb9672017-01-13 15:03:19 +000046 * Clear all static temporary variables.
47 */
48static void clear_temp_vars(void)
49{
50#define ZERO_AND_CLEAN(x) \
51 do { \
Douglas Raillarda8954fc2017-01-26 15:54:44 +000052 zeromem(&x, sizeof(x)); \
Antonio Nino Diaz30eb9672017-01-13 15:03:19 +000053 clean_dcache_range((uintptr_t)&x, sizeof(x)); \
54 } while (0);
55
56 ZERO_AND_CLEAN(tbs)
57 ZERO_AND_CLEAN(v3_ext);
58 ZERO_AND_CLEAN(pk);
59 ZERO_AND_CLEAN(sig_alg);
60 ZERO_AND_CLEAN(signature);
61
62#undef ZERO_AND_CLEAN
63}
64
65/*
Juan Castilloa57a4d52015-04-02 15:44:20 +010066 * Get X509v3 extension
67 *
68 * Global variable 'v3_ext' must point to the extensions region
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -050069 * in the certificate. OID may be NULL to request that get_ext()
70 * is only being called for integrity checking.
Juan Castilloa57a4d52015-04-02 15:44:20 +010071 */
72static int get_ext(const char *oid, void **ext, unsigned int *ext_len)
73{
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -050074 int oid_len, ret, is_critical;
Juan Castilloa57a4d52015-04-02 15:44:20 +010075 size_t len;
Juan Castilloa57a4d52015-04-02 15:44:20 +010076 unsigned char *p;
77 const unsigned char *end;
78 char oid_str[MAX_OID_STR_LEN];
Juan Castillobae6b2a2015-11-05 09:24:53 +000079 mbedtls_asn1_buf extn_oid;
Juan Castilloa57a4d52015-04-02 15:44:20 +010080
81 p = v3_ext.p;
82 end = v3_ext.p + v3_ext.len;
83
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -050084 /*
85 * Check extensions integrity. At least one extension is
86 * required: the ASN.1 specifies a minimum size of 1, and at
87 * least one extension is needed to authenticate the next stage
88 * in the boot chain.
89 */
90 do {
91 unsigned char *end_ext_data;
Juan Castilloa57a4d52015-04-02 15:44:20 +010092
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -050093 ret = mbedtls_asn1_get_tag(&p, end, &len,
94 MBEDTLS_ASN1_CONSTRUCTED |
95 MBEDTLS_ASN1_SEQUENCE);
96 if (ret != 0) {
97 return IMG_PARSER_ERR_FORMAT;
98 }
Juan Castilloa57a4d52015-04-02 15:44:20 +010099 end_ext_data = p + len;
100
101 /* Get extension ID */
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500102 ret = mbedtls_asn1_get_tag(&p, end_ext_data, &extn_oid.len,
103 MBEDTLS_ASN1_OID);
104 if (ret != 0) {
105 return IMG_PARSER_ERR_FORMAT;
106 }
107 extn_oid.tag = MBEDTLS_ASN1_OID;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100108 extn_oid.p = p;
109 p += extn_oid.len;
110
111 /* Get optional critical */
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500112 ret = mbedtls_asn1_get_bool(&p, end_ext_data, &is_critical);
113 if ((ret != 0) && (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
114 return IMG_PARSER_ERR_FORMAT;
115 }
Juan Castilloa57a4d52015-04-02 15:44:20 +0100116
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500117 /*
118 * Data should be octet string type and must use all bytes in
119 * the Extension.
120 */
121 ret = mbedtls_asn1_get_tag(&p, end_ext_data, &len,
122 MBEDTLS_ASN1_OCTET_STRING);
123 if ((ret != 0) || ((p + len) != end_ext_data)) {
124 return IMG_PARSER_ERR_FORMAT;
125 }
Juan Castilloa57a4d52015-04-02 15:44:20 +0100126
127 /* Detect requested extension */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000128 oid_len = mbedtls_oid_get_numeric_string(oid_str,
129 MAX_OID_STR_LEN,
130 &extn_oid);
Nicolas Toromanoff2d817a32020-12-23 16:01:25 +0100131 if ((oid_len == MBEDTLS_ERR_OID_BUF_TOO_SMALL) || (oid_len < 0)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100132 return IMG_PARSER_ERR;
133 }
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500134
135 if ((oid != NULL) &&
136 ((size_t)oid_len == strlen(oid_str)) &&
137 (strcmp(oid, oid_str) == 0)) {
Demi Marie Obenourc2800782023-05-30 13:49:29 -0400138 /* Extension must be ASN.1 DER */
139 if (len < 2) {
140 /* too short */
141 return IMG_PARSER_ERR_FORMAT;
142 }
143
144 if ((p[0] & 0x1F) == 0x1F) {
145 /* multi-byte ASN.1 DER tag, not allowed */
146 return IMG_PARSER_ERR_FORMAT;
147 }
148
149 if ((p[0] & 0xDF) == 0) {
150 /* UNIVERSAL 0 tag, not allowed */
151 return IMG_PARSER_ERR_FORMAT;
152 }
153
Juan Castilloa57a4d52015-04-02 15:44:20 +0100154 *ext = (void *)p;
155 *ext_len = (unsigned int)len;
Demi Marie Obenourc2800782023-05-30 13:49:29 -0400156
157 /* Advance past the tag byte */
158 p++;
159
160 if (mbedtls_asn1_get_len(&p, end_ext_data, &len)) {
161 /* not valid DER */
162 return IMG_PARSER_ERR_FORMAT;
163 }
164
165 if (p + len != end_ext_data) {
166 /* junk after ASN.1 object */
167 return IMG_PARSER_ERR_FORMAT;
168 }
169
Juan Castilloa57a4d52015-04-02 15:44:20 +0100170 return IMG_PARSER_OK;
171 }
172
173 /* Next */
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500174 p = end_ext_data;
175 } while (p < end);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100176
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500177 return (oid == NULL) ? IMG_PARSER_OK : IMG_PARSER_ERR_NOT_FOUND;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100178}
179
180
181/*
182 * Check the integrity of the certificate ASN.1 structure.
Antonio Nino Diaz30eb9672017-01-13 15:03:19 +0000183 *
Juan Castilloa57a4d52015-04-02 15:44:20 +0100184 * Extract the relevant data that will be used later during authentication.
Antonio Nino Diaz30eb9672017-01-13 15:03:19 +0000185 *
186 * This function doesn't clear the static variables located on the top of this
187 * file in case of an error. It is only called from check_integrity(), which
188 * performs the cleanup if necessary.
Juan Castilloa57a4d52015-04-02 15:44:20 +0100189 */
190static int cert_parse(void *img, unsigned int img_len)
191{
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500192 int ret;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100193 size_t len;
Demi Marie Obenourb1342a22022-12-08 15:24:18 -0500194 unsigned char *p, *end, *crt_end, *pk_end;
Demi Marie Obenour139b84c2022-12-08 15:24:23 -0500195 mbedtls_asn1_buf sig_alg1;
Demi Marie Obenour83737b72022-12-08 15:23:50 -0500196 /*
197 * The unique ASN.1 DER encoding of [0] EXPLICIT INTEGER { v3(2} }.
198 */
199 static const char v3[] = {
200 /* The outer CONTEXT SPECIFIC 0 tag */
201 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0,
202 /* The number bytes used to encode the inner INTEGER */
203 3,
204 /* The tag of the inner INTEGER */
205 MBEDTLS_ASN1_INTEGER,
206 /* The number of bytes needed to represent 2 */
207 1,
208 /* The actual value 2 */
209 2,
210 };
Juan Castilloa57a4d52015-04-02 15:44:20 +0100211
212 p = (unsigned char *)img;
213 len = img_len;
Demi Marie Obenour01ea2392023-01-19 09:50:16 -0500214 crt_end = p + len;
215 end = crt_end;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100216
217 /*
218 * Certificate ::= SEQUENCE {
219 * tbsCertificate TBSCertificate,
220 * signatureAlgorithm AlgorithmIdentifier,
221 * signatureValue BIT STRING }
222 */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000223 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
224 MBEDTLS_ASN1_SEQUENCE);
Demi Marie Obenour01ea2392023-01-19 09:50:16 -0500225 if ((ret != 0) || ((p + len) != end)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100226 return IMG_PARSER_ERR_FORMAT;
227 }
228
Juan Castilloa57a4d52015-04-02 15:44:20 +0100229 /*
230 * TBSCertificate ::= SEQUENCE {
231 */
232 tbs.p = p;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000233 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
234 MBEDTLS_ASN1_SEQUENCE);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100235 if (ret != 0) {
236 return IMG_PARSER_ERR_FORMAT;
237 }
238 end = p + len;
239 tbs.len = end - tbs.p;
240
241 /*
Demi Marie Obenour83737b72022-12-08 15:23:50 -0500242 * Version ::= [0] EXPLICIT INTEGER { v1(0), v2(1), v3(2) }
243 * -- only v3 accepted
Juan Castilloa57a4d52015-04-02 15:44:20 +0100244 */
Demi Marie Obenour83737b72022-12-08 15:23:50 -0500245 if (((end - p) <= (ptrdiff_t)sizeof(v3)) ||
246 (memcmp(p, v3, sizeof(v3)) != 0)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100247 return IMG_PARSER_ERR_FORMAT;
248 }
Demi Marie Obenour83737b72022-12-08 15:23:50 -0500249 p += sizeof(v3);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100250
251 /*
252 * CertificateSerialNumber ::= INTEGER
253 */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000254 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100255 if (ret != 0) {
256 return IMG_PARSER_ERR_FORMAT;
257 }
258 p += len;
259
260 /*
261 * signature AlgorithmIdentifier
262 */
263 sig_alg1.p = p;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000264 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
265 MBEDTLS_ASN1_SEQUENCE);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100266 if (ret != 0) {
267 return IMG_PARSER_ERR_FORMAT;
268 }
Juan Castilloa57a4d52015-04-02 15:44:20 +0100269 sig_alg1.len = (p + len) - sig_alg1.p;
270 p += len;
271
272 /*
273 * issuer Name
274 */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000275 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
276 MBEDTLS_ASN1_SEQUENCE);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100277 if (ret != 0) {
278 return IMG_PARSER_ERR_FORMAT;
279 }
280 p += len;
281
282 /*
283 * Validity ::= SEQUENCE {
284 * notBefore Time,
285 * notAfter Time }
286 *
287 */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000288 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
289 MBEDTLS_ASN1_SEQUENCE);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100290 if (ret != 0) {
291 return IMG_PARSER_ERR_FORMAT;
292 }
293 p += len;
294
295 /*
296 * subject Name
297 */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000298 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
299 MBEDTLS_ASN1_SEQUENCE);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100300 if (ret != 0) {
301 return IMG_PARSER_ERR_FORMAT;
302 }
303 p += len;
304
305 /*
306 * SubjectPublicKeyInfo
307 */
308 pk.p = p;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000309 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
310 MBEDTLS_ASN1_SEQUENCE);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100311 if (ret != 0) {
312 return IMG_PARSER_ERR_FORMAT;
313 }
Demi Marie Obenourb1342a22022-12-08 15:24:18 -0500314 pk_end = p + len;
315 pk.len = pk_end - pk.p;
316
Demi Marie Obenour0587d9c2022-12-08 15:24:36 -0500317 /* algorithm */
Demi Marie Obenourb1342a22022-12-08 15:24:18 -0500318 ret = mbedtls_asn1_get_tag(&p, pk_end, &len, MBEDTLS_ASN1_CONSTRUCTED |
319 MBEDTLS_ASN1_SEQUENCE);
320 if (ret != 0) {
321 return IMG_PARSER_ERR_FORMAT;
322 }
Juan Castilloa57a4d52015-04-02 15:44:20 +0100323 p += len;
324
Demi Marie Obenour4d1610b2022-12-08 15:24:27 -0500325 /* Key is a BIT STRING and must use all bytes in SubjectPublicKeyInfo */
326 ret = mbedtls_asn1_get_bitstring_null(&p, pk_end, &len);
Demi Marie Obenourb1342a22022-12-08 15:24:18 -0500327 if ((ret != 0) || (p + len != pk_end)) {
328 return IMG_PARSER_ERR_FORMAT;
329 }
330 p = pk_end;
331
Juan Castilloa57a4d52015-04-02 15:44:20 +0100332 /*
333 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
Juan Castilloa57a4d52015-04-02 15:44:20 +0100334 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
Demi Marie Obenour221a6482023-01-19 09:46:55 -0500335 * -- technically these contain BIT STRINGs but that is not worth
336 * -- validating
Juan Castilloa57a4d52015-04-02 15:44:20 +0100337 */
Demi Marie Obenour221a6482023-01-19 09:46:55 -0500338 for (int i = 1; i < 3; i++) {
339 ret = mbedtls_asn1_get_tag(&p, end, &len,
340 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
341 MBEDTLS_ASN1_CONSTRUCTED | i);
342 /*
343 * Unique IDs are obsolete, so MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
344 * is the common case.
345 */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000346 if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Demi Marie Obenour221a6482023-01-19 09:46:55 -0500347 if (ret != 0) {
348 return IMG_PARSER_ERR_FORMAT;
349 }
350 p += len;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100351 }
Juan Castilloa57a4d52015-04-02 15:44:20 +0100352 }
353
354 /*
355 * extensions [3] EXPLICIT Extensions OPTIONAL
Demi Marie Obenour47a80552022-12-08 15:23:58 -0500356 * }
357 *
358 * X.509 and RFC5280 allow omitting the extensions entirely.
359 * However, in TF-A, a certificate with no extensions would
360 * always fail later on, as the extensions contain the
361 * information needed to authenticate the next stage in the
362 * boot chain. Furthermore, get_ext() assumes that the
363 * extensions have been parsed into v3_ext, and allowing
364 * there to be no extensions would pointlessly complicate
365 * the code. Therefore, just reject certificates without
366 * extensions. This is also why version 1 and 2 certificates
367 * are rejected above.
Juan Castilloa57a4d52015-04-02 15:44:20 +0100368 */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000369 ret = mbedtls_asn1_get_tag(&p, end, &len,
370 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
371 MBEDTLS_ASN1_CONSTRUCTED | 3);
Demi Marie Obenourfa07d042022-12-08 15:23:56 -0500372 if ((ret != 0) || (len != (size_t)(end - p))) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100373 return IMG_PARSER_ERR_FORMAT;
374 }
375
376 /*
377 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Demi Marie Obenourfa07d042022-12-08 15:23:56 -0500378 * -- must use all remaining bytes in TBSCertificate
Juan Castilloa57a4d52015-04-02 15:44:20 +0100379 */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000380 ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
381 MBEDTLS_ASN1_SEQUENCE);
Demi Marie Obenourfa07d042022-12-08 15:23:56 -0500382 if ((ret != 0) || (len != (size_t)(end - p))) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100383 return IMG_PARSER_ERR_FORMAT;
384 }
Demi Marie Obenour0587d9c2022-12-08 15:24:36 -0500385 v3_ext.p = p;
386 v3_ext.len = len;
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500387 p += len;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100388
Demi Marie Obenoure5e03e82023-01-28 15:15:37 -0500389 /* Check extensions integrity */
390 ret = get_ext(NULL, NULL, NULL);
391 if (ret != IMG_PARSER_OK) {
392 return ret;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100393 }
394
395 end = crt_end;
396
397 /*
398 * }
399 * -- end of TBSCertificate
400 *
401 * signatureAlgorithm AlgorithmIdentifier
Demi Marie Obenour139b84c2022-12-08 15:24:23 -0500402 * -- Does not need to be parsed. Ensuring it is bitwise
403 * -- identical (including the tag!) with the first signature
404 * -- algorithm is sufficient.
Juan Castilloa57a4d52015-04-02 15:44:20 +0100405 */
Demi Marie Obenour139b84c2022-12-08 15:24:23 -0500406 if ((sig_alg1.len >= (size_t)(end - p)) ||
407 (0 != memcmp(sig_alg1.p, p, sig_alg1.len))) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100408 return IMG_PARSER_ERR_FORMAT;
409 }
Demi Marie Obenour139b84c2022-12-08 15:24:23 -0500410 p += sig_alg1.len;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100411 memcpy(&sig_alg, &sig_alg1, sizeof(sig_alg));
412
413 /*
414 * signatureValue BIT STRING
Demi Marie Obenour01ea2392023-01-19 09:50:16 -0500415 * } -- must consume all bytes
Juan Castilloa57a4d52015-04-02 15:44:20 +0100416 */
417 signature.p = p;
Demi Marie Obenour4d1610b2022-12-08 15:24:27 -0500418 ret = mbedtls_asn1_get_bitstring_null(&p, end, &len);
Demi Marie Obenour01ea2392023-01-19 09:50:16 -0500419 if ((ret != 0) || ((p + len) != end)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100420 return IMG_PARSER_ERR_FORMAT;
421 }
Demi Marie Obenour01ea2392023-01-19 09:50:16 -0500422 signature.len = end - signature.p;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100423
424 return IMG_PARSER_OK;
425}
426
427
428/* Exported functions */
429
430static void init(void)
431{
432 mbedtls_init();
433}
434
Antonio Nino Diaz30eb9672017-01-13 15:03:19 +0000435/*
436 * Wrapper for cert_parse() that clears the static variables used by it in case
437 * of an error.
438 */
Juan Castilloa57a4d52015-04-02 15:44:20 +0100439static int check_integrity(void *img, unsigned int img_len)
440{
Antonio Nino Diaz30eb9672017-01-13 15:03:19 +0000441 int rc = cert_parse(img, img_len);
442
443 if (rc != IMG_PARSER_OK)
444 clear_temp_vars();
445
446 return rc;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100447}
448
449/*
450 * Extract an authentication parameter from an X509v3 certificate
Juan Castillobfb7fa62016-01-22 11:05:57 +0000451 *
452 * This function returns a pointer to the extracted data and its length.
453 * Depending on the type of parameter, a pointer to the data stored in the
454 * certificate may be returned (i.e. an octet string containing a hash). Other
455 * data may need to be copied and formatted (i.e. integers). In the later case,
456 * a buffer of the correct type needs to be statically allocated, filled and
457 * returned.
Juan Castilloa57a4d52015-04-02 15:44:20 +0100458 */
459static int get_auth_param(const auth_param_type_desc_t *type_desc,
460 void *img, unsigned int img_len,
461 void **param, unsigned int *param_len)
462{
463 int rc = IMG_PARSER_OK;
464
465 /* We do not use img because the check_integrity function has already
466 * extracted the relevant data (v3_ext, pk, sig_alg, etc) */
467
468 switch (type_desc->type) {
469 case AUTH_PARAM_RAW_DATA:
470 /* Data to be signed */
471 *param = (void *)tbs.p;
472 *param_len = (unsigned int)tbs.len;
473 break;
474 case AUTH_PARAM_HASH:
Juan Castillobfb7fa62016-01-22 11:05:57 +0000475 case AUTH_PARAM_NV_CTR:
Juan Castilloa57a4d52015-04-02 15:44:20 +0100476 /* All these parameters are included as X509v3 extensions */
477 rc = get_ext(type_desc->cookie, param, param_len);
478 break;
479 case AUTH_PARAM_PUB_KEY:
Yann Gautierbb7ffdf2023-01-06 11:47:26 +0100480 if (type_desc->cookie != NULL) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100481 /* Get public key from extension */
482 rc = get_ext(type_desc->cookie, param, param_len);
483 } else {
484 /* Get the subject public key */
485 *param = (void *)pk.p;
486 *param_len = (unsigned int)pk.len;
487 }
488 break;
489 case AUTH_PARAM_SIG_ALG:
490 /* Get the certificate signature algorithm */
491 *param = (void *)sig_alg.p;
492 *param_len = (unsigned int)sig_alg.len;
493 break;
494 case AUTH_PARAM_SIG:
495 /* Get the certificate signature */
496 *param = (void *)signature.p;
497 *param_len = (unsigned int)signature.len;
498 break;
499 default:
500 rc = IMG_PARSER_ERR_NOT_FOUND;
501 break;
502 }
503
504 return rc;
505}
506
Elyes Haouas183638f2023-02-13 10:05:41 +0100507REGISTER_IMG_PARSER_LIB(IMG_CERT, LIB_NAME, init,
Juan Castilloa57a4d52015-04-02 15:44:20 +0100508 check_integrity, get_auth_param);