xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_x509_parser.c (revision ddd9f6757e61ca9abe682eeb8d6dc3b16ef1044c)
17d37aa17SJuan Castillo /*
2654b65b3SYann Gautier  * Copyright (c) 2015-2023, ARM Limited and Contributors. All rights reserved.
37d37aa17SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57d37aa17SJuan Castillo  */
67d37aa17SJuan Castillo 
77d37aa17SJuan Castillo /*
8649dbf6fSJuan Castillo  * X509 parser based on mbed TLS
97d37aa17SJuan Castillo  *
107d37aa17SJuan Castillo  * This module implements functions to check the integrity of a X509v3
117d37aa17SJuan Castillo  * certificate ASN.1 structure and extract authentication parameters from the
127d37aa17SJuan Castillo  * extensions field, such as an image hash or a public key.
137d37aa17SJuan Castillo  */
147d37aa17SJuan Castillo 
157d37aa17SJuan Castillo #include <assert.h>
167d37aa17SJuan Castillo #include <stddef.h>
177d37aa17SJuan Castillo #include <stdint.h>
187d37aa17SJuan Castillo #include <string.h>
197d37aa17SJuan Castillo 
207d37aa17SJuan Castillo /* mbed TLS headers */
21649dbf6fSJuan Castillo #include <mbedtls/asn1.h>
22649dbf6fSJuan Castillo #include <mbedtls/oid.h>
23649dbf6fSJuan Castillo #include <mbedtls/platform.h>
247d37aa17SJuan Castillo 
2509d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
2609d40e0eSAntonio Nino Diaz #include <drivers/auth/img_parser_mod.h>
2709d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_common.h>
2809d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2909d40e0eSAntonio Nino Diaz 
307d37aa17SJuan Castillo /* Maximum OID string length ("a.b.c.d.e.f ...") */
317d37aa17SJuan Castillo #define MAX_OID_STR_LEN			64
327d37aa17SJuan Castillo 
337d37aa17SJuan Castillo #define LIB_NAME	"mbed TLS X509v3"
347d37aa17SJuan Castillo 
357d37aa17SJuan Castillo /* Temporary variables to speed up the authentication parameters search. These
367d37aa17SJuan Castillo  * variables are assigned once during the integrity check and used any time an
377d37aa17SJuan Castillo  * authentication parameter is requested, so we do not have to parse the image
387d37aa17SJuan Castillo  * again */
39649dbf6fSJuan Castillo static mbedtls_asn1_buf tbs;
40649dbf6fSJuan Castillo static mbedtls_asn1_buf v3_ext;
41649dbf6fSJuan Castillo static mbedtls_asn1_buf pk;
42649dbf6fSJuan Castillo static mbedtls_asn1_buf sig_alg;
43649dbf6fSJuan Castillo static mbedtls_asn1_buf signature;
447d37aa17SJuan Castillo 
457d37aa17SJuan Castillo /*
4651c5e1a2SAntonio Nino Diaz  * Clear all static temporary variables.
4751c5e1a2SAntonio Nino Diaz  */
4851c5e1a2SAntonio Nino Diaz static void clear_temp_vars(void)
4951c5e1a2SAntonio Nino Diaz {
5051c5e1a2SAntonio Nino Diaz #define ZERO_AND_CLEAN(x)					\
5151c5e1a2SAntonio Nino Diaz 	do {							\
5232f0d3c6SDouglas Raillard 		zeromem(&x, sizeof(x));				\
5351c5e1a2SAntonio Nino Diaz 		clean_dcache_range((uintptr_t)&x, sizeof(x));	\
5451c5e1a2SAntonio Nino Diaz 	} while (0);
5551c5e1a2SAntonio Nino Diaz 
5651c5e1a2SAntonio Nino Diaz 	ZERO_AND_CLEAN(tbs)
5751c5e1a2SAntonio Nino Diaz 	ZERO_AND_CLEAN(v3_ext);
5851c5e1a2SAntonio Nino Diaz 	ZERO_AND_CLEAN(pk);
5951c5e1a2SAntonio Nino Diaz 	ZERO_AND_CLEAN(sig_alg);
6051c5e1a2SAntonio Nino Diaz 	ZERO_AND_CLEAN(signature);
6151c5e1a2SAntonio Nino Diaz 
6251c5e1a2SAntonio Nino Diaz #undef ZERO_AND_CLEAN
6351c5e1a2SAntonio Nino Diaz }
6451c5e1a2SAntonio Nino Diaz 
6551c5e1a2SAntonio Nino Diaz /*
667d37aa17SJuan Castillo  * Get X509v3 extension
677d37aa17SJuan Castillo  *
687d37aa17SJuan Castillo  * Global variable 'v3_ext' must point to the extensions region
697d37aa17SJuan Castillo  * in the certificate. No need to check for errors since the image has passed
707d37aa17SJuan Castillo  * the integrity check.
717d37aa17SJuan Castillo  */
727d37aa17SJuan Castillo static int get_ext(const char *oid, void **ext, unsigned int *ext_len)
737d37aa17SJuan Castillo {
747d37aa17SJuan Castillo 	int oid_len;
757d37aa17SJuan Castillo 	size_t len;
767d37aa17SJuan Castillo 	unsigned char *end_ext_data, *end_ext_octet;
777d37aa17SJuan Castillo 	unsigned char *p;
787d37aa17SJuan Castillo 	const unsigned char *end;
797d37aa17SJuan Castillo 	char oid_str[MAX_OID_STR_LEN];
80649dbf6fSJuan Castillo 	mbedtls_asn1_buf extn_oid;
817d37aa17SJuan Castillo 	int is_critical;
827d37aa17SJuan Castillo 
837d37aa17SJuan Castillo 	assert(oid != NULL);
847d37aa17SJuan Castillo 
857d37aa17SJuan Castillo 	p = v3_ext.p;
867d37aa17SJuan Castillo 	end = v3_ext.p + v3_ext.len;
877d37aa17SJuan Castillo 
887d37aa17SJuan Castillo 	while (p < end) {
8932f0d3c6SDouglas Raillard 		zeromem(&extn_oid, sizeof(extn_oid));
907d37aa17SJuan Castillo 		is_critical = 0; /* DEFAULT FALSE */
917d37aa17SJuan Castillo 
92649dbf6fSJuan Castillo 		mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
93649dbf6fSJuan Castillo 				     MBEDTLS_ASN1_SEQUENCE);
947d37aa17SJuan Castillo 		end_ext_data = p + len;
957d37aa17SJuan Castillo 
967d37aa17SJuan Castillo 		/* Get extension ID */
977d37aa17SJuan Castillo 		extn_oid.tag = *p;
98649dbf6fSJuan Castillo 		mbedtls_asn1_get_tag(&p, end, &extn_oid.len, MBEDTLS_ASN1_OID);
997d37aa17SJuan Castillo 		extn_oid.p = p;
1007d37aa17SJuan Castillo 		p += extn_oid.len;
1017d37aa17SJuan Castillo 
1027d37aa17SJuan Castillo 		/* Get optional critical */
103649dbf6fSJuan Castillo 		mbedtls_asn1_get_bool(&p, end_ext_data, &is_critical);
1047d37aa17SJuan Castillo 
1057d37aa17SJuan Castillo 		/* Extension data */
106649dbf6fSJuan Castillo 		mbedtls_asn1_get_tag(&p, end_ext_data, &len,
107649dbf6fSJuan Castillo 				     MBEDTLS_ASN1_OCTET_STRING);
1087d37aa17SJuan Castillo 		end_ext_octet = p + len;
1097d37aa17SJuan Castillo 
1107d37aa17SJuan Castillo 		/* Detect requested extension */
111649dbf6fSJuan Castillo 		oid_len = mbedtls_oid_get_numeric_string(oid_str,
112649dbf6fSJuan Castillo 							 MAX_OID_STR_LEN,
113649dbf6fSJuan Castillo 							 &extn_oid);
114ed38366fSNicolas Toromanoff 		if ((oid_len == MBEDTLS_ERR_OID_BUF_TOO_SMALL) || (oid_len < 0)) {
1157d37aa17SJuan Castillo 			return IMG_PARSER_ERR;
1167d37aa17SJuan Castillo 		}
117ed38366fSNicolas Toromanoff 		if (((size_t)oid_len == strlen(oid_str)) && !strcmp(oid, oid_str)) {
1187d37aa17SJuan Castillo 			*ext = (void *)p;
1197d37aa17SJuan Castillo 			*ext_len = (unsigned int)len;
1207d37aa17SJuan Castillo 			return IMG_PARSER_OK;
1217d37aa17SJuan Castillo 		}
1227d37aa17SJuan Castillo 
1237d37aa17SJuan Castillo 		/* Next */
1247d37aa17SJuan Castillo 		p = end_ext_octet;
1257d37aa17SJuan Castillo 	}
1267d37aa17SJuan Castillo 
1277d37aa17SJuan Castillo 	return IMG_PARSER_ERR_NOT_FOUND;
1287d37aa17SJuan Castillo }
1297d37aa17SJuan Castillo 
1307d37aa17SJuan Castillo 
1317d37aa17SJuan Castillo /*
1327d37aa17SJuan Castillo  * Check the integrity of the certificate ASN.1 structure.
13351c5e1a2SAntonio Nino Diaz  *
1347d37aa17SJuan Castillo  * Extract the relevant data that will be used later during authentication.
13551c5e1a2SAntonio Nino Diaz  *
13651c5e1a2SAntonio Nino Diaz  * This function doesn't clear the static variables located on the top of this
13751c5e1a2SAntonio Nino Diaz  * file in case of an error. It is only called from check_integrity(), which
13851c5e1a2SAntonio Nino Diaz  * performs the cleanup if necessary.
1397d37aa17SJuan Castillo  */
1407d37aa17SJuan Castillo static int cert_parse(void *img, unsigned int img_len)
1417d37aa17SJuan Castillo {
1427d37aa17SJuan Castillo 	int ret, is_critical;
1437d37aa17SJuan Castillo 	size_t len;
14494c0cfbbSDemi Marie Obenour 	unsigned char *p, *end, *crt_end, *pk_end;
14563cc49d0SDemi Marie Obenour 	mbedtls_asn1_buf sig_alg1;
146e9e4a2a6SDemi Marie Obenour 	/*
147e9e4a2a6SDemi Marie Obenour 	 * The unique ASN.1 DER encoding of [0] EXPLICIT INTEGER { v3(2} }.
148e9e4a2a6SDemi Marie Obenour 	 */
149e9e4a2a6SDemi Marie Obenour 	static const char v3[] = {
150e9e4a2a6SDemi Marie Obenour 		/* The outer CONTEXT SPECIFIC 0 tag */
151e9e4a2a6SDemi Marie Obenour 		MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0,
152e9e4a2a6SDemi Marie Obenour 		/* The number bytes used to encode the inner INTEGER */
153e9e4a2a6SDemi Marie Obenour 		3,
154e9e4a2a6SDemi Marie Obenour 		/* The tag of the inner INTEGER */
155e9e4a2a6SDemi Marie Obenour 		MBEDTLS_ASN1_INTEGER,
156e9e4a2a6SDemi Marie Obenour 		/* The number of bytes needed to represent 2 */
157e9e4a2a6SDemi Marie Obenour 		1,
158e9e4a2a6SDemi Marie Obenour 		/* The actual value 2 */
159e9e4a2a6SDemi Marie Obenour 		2,
160e9e4a2a6SDemi Marie Obenour 	};
1617d37aa17SJuan Castillo 
1627d37aa17SJuan Castillo 	p = (unsigned char *)img;
1637d37aa17SJuan Castillo 	len = img_len;
164*ddd9f675SDemi Marie Obenour 	crt_end = p + len;
165*ddd9f675SDemi Marie Obenour 	end = crt_end;
1667d37aa17SJuan Castillo 
1677d37aa17SJuan Castillo 	/*
1687d37aa17SJuan Castillo 	 * Certificate  ::=  SEQUENCE  {
1697d37aa17SJuan Castillo 	 *      tbsCertificate       TBSCertificate,
1707d37aa17SJuan Castillo 	 *      signatureAlgorithm   AlgorithmIdentifier,
1717d37aa17SJuan Castillo 	 *      signatureValue       BIT STRING  }
1727d37aa17SJuan Castillo 	 */
173649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
174649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_SEQUENCE);
175*ddd9f675SDemi Marie Obenour 	if ((ret != 0) || ((p + len) != end)) {
1767d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
1777d37aa17SJuan Castillo 	}
1787d37aa17SJuan Castillo 
1797d37aa17SJuan Castillo 	/*
1807d37aa17SJuan Castillo 	 * TBSCertificate  ::=  SEQUENCE  {
1817d37aa17SJuan Castillo 	 */
1827d37aa17SJuan Castillo 	tbs.p = p;
183649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
184649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_SEQUENCE);
1857d37aa17SJuan Castillo 	if (ret != 0) {
1867d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
1877d37aa17SJuan Castillo 	}
1887d37aa17SJuan Castillo 	end = p + len;
1897d37aa17SJuan Castillo 	tbs.len = end - tbs.p;
1907d37aa17SJuan Castillo 
1917d37aa17SJuan Castillo 	/*
192e9e4a2a6SDemi Marie Obenour 	 * Version  ::=  [0] EXPLICIT INTEGER {  v1(0), v2(1), v3(2)  }
193e9e4a2a6SDemi Marie Obenour 	 * -- only v3 accepted
1947d37aa17SJuan Castillo 	 */
195e9e4a2a6SDemi Marie Obenour 	if (((end - p) <= (ptrdiff_t)sizeof(v3)) ||
196e9e4a2a6SDemi Marie Obenour 	    (memcmp(p, v3, sizeof(v3)) != 0)) {
1977d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
1987d37aa17SJuan Castillo 	}
199e9e4a2a6SDemi Marie Obenour 	p += sizeof(v3);
2007d37aa17SJuan Castillo 
2017d37aa17SJuan Castillo 	/*
2027d37aa17SJuan Castillo 	 * CertificateSerialNumber  ::=  INTEGER
2037d37aa17SJuan Castillo 	 */
204649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER);
2057d37aa17SJuan Castillo 	if (ret != 0) {
2067d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
2077d37aa17SJuan Castillo 	}
2087d37aa17SJuan Castillo 	p += len;
2097d37aa17SJuan Castillo 
2107d37aa17SJuan Castillo 	/*
2117d37aa17SJuan Castillo 	 * signature            AlgorithmIdentifier
2127d37aa17SJuan Castillo 	 */
2137d37aa17SJuan Castillo 	sig_alg1.p = p;
214649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
215649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_SEQUENCE);
2167d37aa17SJuan Castillo 	if (ret != 0) {
2177d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
2187d37aa17SJuan Castillo 	}
2197d37aa17SJuan Castillo 	sig_alg1.len = (p + len) - sig_alg1.p;
2207d37aa17SJuan Castillo 	p += len;
2217d37aa17SJuan Castillo 
2227d37aa17SJuan Castillo 	/*
2237d37aa17SJuan Castillo 	 * issuer               Name
2247d37aa17SJuan Castillo 	 */
225649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
226649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_SEQUENCE);
2277d37aa17SJuan Castillo 	if (ret != 0) {
2287d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
2297d37aa17SJuan Castillo 	}
2307d37aa17SJuan Castillo 	p += len;
2317d37aa17SJuan Castillo 
2327d37aa17SJuan Castillo 	/*
2337d37aa17SJuan Castillo 	 * Validity ::= SEQUENCE {
2347d37aa17SJuan Castillo 	 *      notBefore      Time,
2357d37aa17SJuan Castillo 	 *      notAfter       Time }
2367d37aa17SJuan Castillo 	 *
2377d37aa17SJuan Castillo 	 */
238649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
239649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_SEQUENCE);
2407d37aa17SJuan Castillo 	if (ret != 0) {
2417d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
2427d37aa17SJuan Castillo 	}
2437d37aa17SJuan Castillo 	p += len;
2447d37aa17SJuan Castillo 
2457d37aa17SJuan Castillo 	/*
2467d37aa17SJuan Castillo 	 * subject              Name
2477d37aa17SJuan Castillo 	 */
248649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
249649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_SEQUENCE);
2507d37aa17SJuan Castillo 	if (ret != 0) {
2517d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
2527d37aa17SJuan Castillo 	}
2537d37aa17SJuan Castillo 	p += len;
2547d37aa17SJuan Castillo 
2557d37aa17SJuan Castillo 	/*
2567d37aa17SJuan Castillo 	 * SubjectPublicKeyInfo
2577d37aa17SJuan Castillo 	 */
2587d37aa17SJuan Castillo 	pk.p = p;
259649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
260649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_SEQUENCE);
2617d37aa17SJuan Castillo 	if (ret != 0) {
2627d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
2637d37aa17SJuan Castillo 	}
26494c0cfbbSDemi Marie Obenour 	pk_end = p + len;
26594c0cfbbSDemi Marie Obenour 	pk.len = pk_end - pk.p;
26694c0cfbbSDemi Marie Obenour 
267ce882b53SDemi Marie Obenour 	/* algorithm */
26894c0cfbbSDemi Marie Obenour 	ret = mbedtls_asn1_get_tag(&p, pk_end, &len, MBEDTLS_ASN1_CONSTRUCTED |
26994c0cfbbSDemi Marie Obenour 				   MBEDTLS_ASN1_SEQUENCE);
27094c0cfbbSDemi Marie Obenour 	if (ret != 0) {
27194c0cfbbSDemi Marie Obenour 		return IMG_PARSER_ERR_FORMAT;
27294c0cfbbSDemi Marie Obenour 	}
2737d37aa17SJuan Castillo 	p += len;
2747d37aa17SJuan Castillo 
2758816dbb3SDemi Marie Obenour 	/* Key is a BIT STRING and must use all bytes in SubjectPublicKeyInfo */
2768816dbb3SDemi Marie Obenour 	ret = mbedtls_asn1_get_bitstring_null(&p, pk_end, &len);
27794c0cfbbSDemi Marie Obenour 	if ((ret != 0) || (p + len != pk_end)) {
27894c0cfbbSDemi Marie Obenour 		return IMG_PARSER_ERR_FORMAT;
27994c0cfbbSDemi Marie Obenour 	}
28094c0cfbbSDemi Marie Obenour 	p = pk_end;
28194c0cfbbSDemi Marie Obenour 
2827d37aa17SJuan Castillo 	/*
2837d37aa17SJuan Castillo 	 * issuerUniqueID  [1]  IMPLICIT UniqueIdentifier OPTIONAL,
2847d37aa17SJuan Castillo 	 * subjectUniqueID [2]  IMPLICIT UniqueIdentifier OPTIONAL,
2856a7104a3SDemi Marie Obenour 	 * -- technically these contain BIT STRINGs but that is not worth
2866a7104a3SDemi Marie Obenour 	 * -- validating
2877d37aa17SJuan Castillo 	 */
2886a7104a3SDemi Marie Obenour 	for (int i = 1; i < 3; i++) {
289649dbf6fSJuan Castillo 		ret = mbedtls_asn1_get_tag(&p, end, &len,
290649dbf6fSJuan Castillo 					   MBEDTLS_ASN1_CONTEXT_SPECIFIC |
2916a7104a3SDemi Marie Obenour 					   MBEDTLS_ASN1_CONSTRUCTED | i);
2926a7104a3SDemi Marie Obenour 		/*
2936a7104a3SDemi Marie Obenour 		 * Unique IDs are obsolete, so MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
2946a7104a3SDemi Marie Obenour 		 * is the common case.
2956a7104a3SDemi Marie Obenour 		 */
296649dbf6fSJuan Castillo 		if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
2976a7104a3SDemi Marie Obenour 			if (ret != 0) {
2987d37aa17SJuan Castillo 				return IMG_PARSER_ERR_FORMAT;
2997d37aa17SJuan Castillo 			}
3007d37aa17SJuan Castillo 			p += len;
3017d37aa17SJuan Castillo 		}
3026a7104a3SDemi Marie Obenour 	}
3037d37aa17SJuan Castillo 
3047d37aa17SJuan Castillo 	/*
3057d37aa17SJuan Castillo 	 * extensions      [3]  EXPLICIT Extensions OPTIONAL
30672460f50SDemi Marie Obenour 	 * }
30772460f50SDemi Marie Obenour 	 *
30872460f50SDemi Marie Obenour 	 * X.509 and RFC5280 allow omitting the extensions entirely.
30972460f50SDemi Marie Obenour 	 * However, in TF-A, a certificate with no extensions would
31072460f50SDemi Marie Obenour 	 * always fail later on, as the extensions contain the
31172460f50SDemi Marie Obenour 	 * information needed to authenticate the next stage in the
31272460f50SDemi Marie Obenour 	 * boot chain.  Furthermore, get_ext() assumes that the
31372460f50SDemi Marie Obenour 	 * extensions have been parsed into v3_ext, and allowing
31472460f50SDemi Marie Obenour 	 * there to be no extensions would pointlessly complicate
31572460f50SDemi Marie Obenour 	 * the code.  Therefore, just reject certificates without
31672460f50SDemi Marie Obenour 	 * extensions.  This is also why version 1 and 2 certificates
31772460f50SDemi Marie Obenour 	 * are rejected above.
3187d37aa17SJuan Castillo 	 */
319649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len,
320649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_CONTEXT_SPECIFIC |
321649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_CONSTRUCTED | 3);
322fd37982aSDemi Marie Obenour 	if ((ret != 0) || (len != (size_t)(end - p))) {
3237d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
3247d37aa17SJuan Castillo 	}
3257d37aa17SJuan Castillo 
3267d37aa17SJuan Castillo 	/*
3277d37aa17SJuan Castillo 	 * Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
328fd37982aSDemi Marie Obenour 	 * -- must use all remaining bytes in TBSCertificate
3297d37aa17SJuan Castillo 	 */
330649dbf6fSJuan Castillo 	ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
331649dbf6fSJuan Castillo 				   MBEDTLS_ASN1_SEQUENCE);
332fd37982aSDemi Marie Obenour 	if ((ret != 0) || (len != (size_t)(end - p))) {
3337d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
3347d37aa17SJuan Castillo 	}
335ce882b53SDemi Marie Obenour 	v3_ext.p = p;
336ce882b53SDemi Marie Obenour 	v3_ext.len = len;
3377d37aa17SJuan Castillo 
3387d37aa17SJuan Castillo 	/*
33972460f50SDemi Marie Obenour 	 * Check extensions integrity.  At least one extension is
34072460f50SDemi Marie Obenour 	 * required: the ASN.1 specifies a minimum size of 1, and at
34172460f50SDemi Marie Obenour 	 * least one extension is needed to authenticate the next stage
34272460f50SDemi Marie Obenour 	 * in the boot chain.
3437d37aa17SJuan Castillo 	 */
34472460f50SDemi Marie Obenour 	do {
345f5c51855SDemi Marie Obenour 		unsigned char *end_ext_data;
346f5c51855SDemi Marie Obenour 
347649dbf6fSJuan Castillo 		ret = mbedtls_asn1_get_tag(&p, end, &len,
348649dbf6fSJuan Castillo 					   MBEDTLS_ASN1_CONSTRUCTED |
349649dbf6fSJuan Castillo 					   MBEDTLS_ASN1_SEQUENCE);
3507d37aa17SJuan Castillo 		if (ret != 0) {
3517d37aa17SJuan Castillo 			return IMG_PARSER_ERR_FORMAT;
3527d37aa17SJuan Castillo 		}
353f5c51855SDemi Marie Obenour 		end_ext_data = p + len;
3547d37aa17SJuan Castillo 
3557d37aa17SJuan Castillo 		/* Get extension ID */
356f5c51855SDemi Marie Obenour 		ret = mbedtls_asn1_get_tag(&p, end_ext_data, &len, MBEDTLS_ASN1_OID);
3577d37aa17SJuan Castillo 		if (ret != 0) {
3587d37aa17SJuan Castillo 			return IMG_PARSER_ERR_FORMAT;
3597d37aa17SJuan Castillo 		}
3607d37aa17SJuan Castillo 		p += len;
3617d37aa17SJuan Castillo 
3627d37aa17SJuan Castillo 		/* Get optional critical */
363f5c51855SDemi Marie Obenour 		ret = mbedtls_asn1_get_bool(&p, end_ext_data, &is_critical);
364649dbf6fSJuan Castillo 		if ((ret != 0) && (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
3657d37aa17SJuan Castillo 			return IMG_PARSER_ERR_FORMAT;
3667d37aa17SJuan Castillo 		}
3677d37aa17SJuan Castillo 
368f5c51855SDemi Marie Obenour 		/*
369f5c51855SDemi Marie Obenour 		 * Data should be octet string type and must use all bytes in
370f5c51855SDemi Marie Obenour 		 * the Extension.
371f5c51855SDemi Marie Obenour 		 */
372f5c51855SDemi Marie Obenour 		ret = mbedtls_asn1_get_tag(&p, end_ext_data, &len,
373649dbf6fSJuan Castillo 					   MBEDTLS_ASN1_OCTET_STRING);
374f5c51855SDemi Marie Obenour 		if ((ret != 0) || ((p + len) != end_ext_data)) {
3757d37aa17SJuan Castillo 			return IMG_PARSER_ERR_FORMAT;
3767d37aa17SJuan Castillo 		}
377f5c51855SDemi Marie Obenour 		p = end_ext_data;
37872460f50SDemi Marie Obenour 	} while (p < end);
3797d37aa17SJuan Castillo 
3807d37aa17SJuan Castillo 	if (p != end) {
3817d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
3827d37aa17SJuan Castillo 	}
3837d37aa17SJuan Castillo 
3847d37aa17SJuan Castillo 	end = crt_end;
3857d37aa17SJuan Castillo 
3867d37aa17SJuan Castillo 	/*
3877d37aa17SJuan Castillo 	 *  }
3887d37aa17SJuan Castillo 	 *  -- end of TBSCertificate
3897d37aa17SJuan Castillo 	 *
3907d37aa17SJuan Castillo 	 *  signatureAlgorithm   AlgorithmIdentifier
39163cc49d0SDemi Marie Obenour 	 *  -- Does not need to be parsed.  Ensuring it is bitwise
39263cc49d0SDemi Marie Obenour 	 *  -- identical (including the tag!) with the first signature
39363cc49d0SDemi Marie Obenour 	 *  -- algorithm is sufficient.
3947d37aa17SJuan Castillo 	 */
39563cc49d0SDemi Marie Obenour 	if ((sig_alg1.len >= (size_t)(end - p)) ||
39663cc49d0SDemi Marie Obenour 	    (0 != memcmp(sig_alg1.p, p, sig_alg1.len))) {
3977d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
3987d37aa17SJuan Castillo 	}
39963cc49d0SDemi Marie Obenour 	p += sig_alg1.len;
4007d37aa17SJuan Castillo 	memcpy(&sig_alg, &sig_alg1, sizeof(sig_alg));
4017d37aa17SJuan Castillo 
4027d37aa17SJuan Castillo 	/*
4037d37aa17SJuan Castillo 	 * signatureValue       BIT STRING
404*ddd9f675SDemi Marie Obenour 	 * } -- must consume all bytes
4057d37aa17SJuan Castillo 	 */
4067d37aa17SJuan Castillo 	signature.p = p;
4078816dbb3SDemi Marie Obenour 	ret = mbedtls_asn1_get_bitstring_null(&p, end, &len);
408*ddd9f675SDemi Marie Obenour 	if ((ret != 0) || ((p + len) != end)) {
4097d37aa17SJuan Castillo 		return IMG_PARSER_ERR_FORMAT;
4107d37aa17SJuan Castillo 	}
411*ddd9f675SDemi Marie Obenour 	signature.len = end - signature.p;
4127d37aa17SJuan Castillo 
4137d37aa17SJuan Castillo 	return IMG_PARSER_OK;
4147d37aa17SJuan Castillo }
4157d37aa17SJuan Castillo 
4167d37aa17SJuan Castillo 
4177d37aa17SJuan Castillo /* Exported functions */
4187d37aa17SJuan Castillo 
4197d37aa17SJuan Castillo static void init(void)
4207d37aa17SJuan Castillo {
4217d37aa17SJuan Castillo 	mbedtls_init();
4227d37aa17SJuan Castillo }
4237d37aa17SJuan Castillo 
42451c5e1a2SAntonio Nino Diaz /*
42551c5e1a2SAntonio Nino Diaz  * Wrapper for cert_parse() that clears the static variables used by it in case
42651c5e1a2SAntonio Nino Diaz  * of an error.
42751c5e1a2SAntonio Nino Diaz  */
4287d37aa17SJuan Castillo static int check_integrity(void *img, unsigned int img_len)
4297d37aa17SJuan Castillo {
43051c5e1a2SAntonio Nino Diaz 	int rc = cert_parse(img, img_len);
43151c5e1a2SAntonio Nino Diaz 
43251c5e1a2SAntonio Nino Diaz 	if (rc != IMG_PARSER_OK)
43351c5e1a2SAntonio Nino Diaz 		clear_temp_vars();
43451c5e1a2SAntonio Nino Diaz 
43551c5e1a2SAntonio Nino Diaz 	return rc;
4367d37aa17SJuan Castillo }
4377d37aa17SJuan Castillo 
4387d37aa17SJuan Castillo /*
4397d37aa17SJuan Castillo  * Extract an authentication parameter from an X509v3 certificate
44048279d52SJuan Castillo  *
44148279d52SJuan Castillo  * This function returns a pointer to the extracted data and its length.
44248279d52SJuan Castillo  * Depending on the type of parameter, a pointer to the data stored in the
44348279d52SJuan Castillo  * certificate may be returned (i.e. an octet string containing a hash). Other
44448279d52SJuan Castillo  * data may need to be copied and formatted (i.e. integers). In the later case,
44548279d52SJuan Castillo  * a buffer of the correct type needs to be statically allocated, filled and
44648279d52SJuan Castillo  * returned.
4477d37aa17SJuan Castillo  */
4487d37aa17SJuan Castillo static int get_auth_param(const auth_param_type_desc_t *type_desc,
4497d37aa17SJuan Castillo 		void *img, unsigned int img_len,
4507d37aa17SJuan Castillo 		void **param, unsigned int *param_len)
4517d37aa17SJuan Castillo {
4527d37aa17SJuan Castillo 	int rc = IMG_PARSER_OK;
4537d37aa17SJuan Castillo 
4547d37aa17SJuan Castillo 	/* We do not use img because the check_integrity function has already
4557d37aa17SJuan Castillo 	 * extracted the relevant data (v3_ext, pk, sig_alg, etc) */
4567d37aa17SJuan Castillo 
4577d37aa17SJuan Castillo 	switch (type_desc->type) {
4587d37aa17SJuan Castillo 	case AUTH_PARAM_RAW_DATA:
4597d37aa17SJuan Castillo 		/* Data to be signed */
4607d37aa17SJuan Castillo 		*param = (void *)tbs.p;
4617d37aa17SJuan Castillo 		*param_len = (unsigned int)tbs.len;
4627d37aa17SJuan Castillo 		break;
4637d37aa17SJuan Castillo 	case AUTH_PARAM_HASH:
46448279d52SJuan Castillo 	case AUTH_PARAM_NV_CTR:
4657d37aa17SJuan Castillo 		/* All these parameters are included as X509v3 extensions */
4667d37aa17SJuan Castillo 		rc = get_ext(type_desc->cookie, param, param_len);
4677d37aa17SJuan Castillo 		break;
4687d37aa17SJuan Castillo 	case AUTH_PARAM_PUB_KEY:
469654b65b3SYann Gautier 		if (type_desc->cookie != NULL) {
4707d37aa17SJuan Castillo 			/* Get public key from extension */
4717d37aa17SJuan Castillo 			rc = get_ext(type_desc->cookie, param, param_len);
4727d37aa17SJuan Castillo 		} else {
4737d37aa17SJuan Castillo 			/* Get the subject public key */
4747d37aa17SJuan Castillo 			*param = (void *)pk.p;
4757d37aa17SJuan Castillo 			*param_len = (unsigned int)pk.len;
4767d37aa17SJuan Castillo 		}
4777d37aa17SJuan Castillo 		break;
4787d37aa17SJuan Castillo 	case AUTH_PARAM_SIG_ALG:
4797d37aa17SJuan Castillo 		/* Get the certificate signature algorithm */
4807d37aa17SJuan Castillo 		*param = (void *)sig_alg.p;
4817d37aa17SJuan Castillo 		*param_len = (unsigned int)sig_alg.len;
4827d37aa17SJuan Castillo 		break;
4837d37aa17SJuan Castillo 	case AUTH_PARAM_SIG:
4847d37aa17SJuan Castillo 		/* Get the certificate signature */
4857d37aa17SJuan Castillo 		*param = (void *)signature.p;
4867d37aa17SJuan Castillo 		*param_len = (unsigned int)signature.len;
4877d37aa17SJuan Castillo 		break;
4887d37aa17SJuan Castillo 	default:
4897d37aa17SJuan Castillo 		rc = IMG_PARSER_ERR_NOT_FOUND;
4907d37aa17SJuan Castillo 		break;
4917d37aa17SJuan Castillo 	}
4927d37aa17SJuan Castillo 
4937d37aa17SJuan Castillo 	return rc;
4947d37aa17SJuan Castillo }
4957d37aa17SJuan Castillo 
4967d37aa17SJuan Castillo REGISTER_IMG_PARSER_LIB(IMG_CERT, LIB_NAME, init, \
4977d37aa17SJuan Castillo 		       check_integrity, get_auth_param);
498