Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | /* |
Nicolas Toromanoff | 2d817a3 | 2020-12-23 16:01:25 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, ARM Limited and Contributors. All rights reserved. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 8 | * X509 parser based on mbed TLS |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 9 | * |
| 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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 16 | #include <stddef.h> |
| 17 | #include <stdint.h> |
| 18 | #include <string.h> |
| 19 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 20 | /* mbed TLS headers */ |
| 21 | #include <mbedtls/asn1.h> |
| 22 | #include <mbedtls/oid.h> |
| 23 | #include <mbedtls/platform.h> |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 24 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 25 | #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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 30 | /* Maximum OID string length ("a.b.c.d.e.f ...") */ |
| 31 | #define MAX_OID_STR_LEN 64 |
| 32 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 33 | #define LIB_NAME "mbed TLS X509v3" |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 34 | |
| 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 Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 39 | static mbedtls_asn1_buf tbs; |
| 40 | static mbedtls_asn1_buf v3_ext; |
| 41 | static mbedtls_asn1_buf pk; |
| 42 | static mbedtls_asn1_buf sig_alg; |
| 43 | static mbedtls_asn1_buf signature; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 44 | |
| 45 | /* |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 46 | * Clear all static temporary variables. |
| 47 | */ |
| 48 | static void clear_temp_vars(void) |
| 49 | { |
| 50 | #define ZERO_AND_CLEAN(x) \ |
| 51 | do { \ |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 52 | zeromem(&x, sizeof(x)); \ |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 53 | 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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 66 | * Get X509v3 extension |
| 67 | * |
| 68 | * Global variable 'v3_ext' must point to the extensions region |
| 69 | * in the certificate. No need to check for errors since the image has passed |
| 70 | * the integrity check. |
| 71 | */ |
| 72 | static int get_ext(const char *oid, void **ext, unsigned int *ext_len) |
| 73 | { |
| 74 | int oid_len; |
| 75 | size_t len; |
| 76 | unsigned char *end_ext_data, *end_ext_octet; |
| 77 | unsigned char *p; |
| 78 | const unsigned char *end; |
| 79 | char oid_str[MAX_OID_STR_LEN]; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 80 | mbedtls_asn1_buf extn_oid; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 81 | int is_critical; |
| 82 | |
| 83 | assert(oid != NULL); |
| 84 | |
| 85 | p = v3_ext.p; |
| 86 | end = v3_ext.p + v3_ext.len; |
| 87 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 88 | while (p < end) { |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 89 | zeromem(&extn_oid, sizeof(extn_oid)); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 90 | is_critical = 0; /* DEFAULT FALSE */ |
| 91 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 92 | mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 93 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 94 | end_ext_data = p + len; |
| 95 | |
| 96 | /* Get extension ID */ |
| 97 | extn_oid.tag = *p; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 98 | mbedtls_asn1_get_tag(&p, end, &extn_oid.len, MBEDTLS_ASN1_OID); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 99 | extn_oid.p = p; |
| 100 | p += extn_oid.len; |
| 101 | |
| 102 | /* Get optional critical */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 103 | mbedtls_asn1_get_bool(&p, end_ext_data, &is_critical); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 104 | |
| 105 | /* Extension data */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 106 | mbedtls_asn1_get_tag(&p, end_ext_data, &len, |
| 107 | MBEDTLS_ASN1_OCTET_STRING); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 108 | end_ext_octet = p + len; |
| 109 | |
| 110 | /* Detect requested extension */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 111 | oid_len = mbedtls_oid_get_numeric_string(oid_str, |
| 112 | MAX_OID_STR_LEN, |
| 113 | &extn_oid); |
Nicolas Toromanoff | 2d817a3 | 2020-12-23 16:01:25 +0100 | [diff] [blame] | 114 | if ((oid_len == MBEDTLS_ERR_OID_BUF_TOO_SMALL) || (oid_len < 0)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 115 | return IMG_PARSER_ERR; |
| 116 | } |
Nicolas Toromanoff | 2d817a3 | 2020-12-23 16:01:25 +0100 | [diff] [blame] | 117 | if (((size_t)oid_len == strlen(oid_str)) && !strcmp(oid, oid_str)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 118 | *ext = (void *)p; |
| 119 | *ext_len = (unsigned int)len; |
| 120 | return IMG_PARSER_OK; |
| 121 | } |
| 122 | |
| 123 | /* Next */ |
| 124 | p = end_ext_octet; |
| 125 | } |
| 126 | |
| 127 | return IMG_PARSER_ERR_NOT_FOUND; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* |
| 132 | * Check the integrity of the certificate ASN.1 structure. |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 133 | * |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 134 | * Extract the relevant data that will be used later during authentication. |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 135 | * |
| 136 | * This function doesn't clear the static variables located on the top of this |
| 137 | * file in case of an error. It is only called from check_integrity(), which |
| 138 | * performs the cleanup if necessary. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 139 | */ |
| 140 | static int cert_parse(void *img, unsigned int img_len) |
| 141 | { |
| 142 | int ret, is_critical; |
| 143 | size_t len; |
Demi Marie Obenour | b1342a2 | 2022-12-08 15:24:18 -0500 | [diff] [blame] | 144 | unsigned char *p, *end, *crt_end, *pk_end; |
Demi Marie Obenour | 139b84c | 2022-12-08 15:24:23 -0500 | [diff] [blame] | 145 | mbedtls_asn1_buf sig_alg1; |
Demi Marie Obenour | 83737b7 | 2022-12-08 15:23:50 -0500 | [diff] [blame] | 146 | /* |
| 147 | * The unique ASN.1 DER encoding of [0] EXPLICIT INTEGER { v3(2} }. |
| 148 | */ |
| 149 | static const char v3[] = { |
| 150 | /* The outer CONTEXT SPECIFIC 0 tag */ |
| 151 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0, |
| 152 | /* The number bytes used to encode the inner INTEGER */ |
| 153 | 3, |
| 154 | /* The tag of the inner INTEGER */ |
| 155 | MBEDTLS_ASN1_INTEGER, |
| 156 | /* The number of bytes needed to represent 2 */ |
| 157 | 1, |
| 158 | /* The actual value 2 */ |
| 159 | 2, |
| 160 | }; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 161 | |
| 162 | p = (unsigned char *)img; |
| 163 | len = img_len; |
| 164 | end = p + len; |
| 165 | |
| 166 | /* |
| 167 | * Certificate ::= SEQUENCE { |
| 168 | * tbsCertificate TBSCertificate, |
| 169 | * signatureAlgorithm AlgorithmIdentifier, |
| 170 | * signatureValue BIT STRING } |
| 171 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 172 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 173 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 174 | if (ret != 0) { |
| 175 | return IMG_PARSER_ERR_FORMAT; |
| 176 | } |
| 177 | |
Demi Marie Obenour | bfff4d9 | 2022-12-08 15:24:31 -0500 | [diff] [blame] | 178 | if (len != (size_t)(end - p)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 179 | return IMG_PARSER_ERR_FORMAT; |
| 180 | } |
| 181 | crt_end = p + len; |
| 182 | |
| 183 | /* |
| 184 | * TBSCertificate ::= SEQUENCE { |
| 185 | */ |
| 186 | tbs.p = p; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 187 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 188 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 189 | if (ret != 0) { |
| 190 | return IMG_PARSER_ERR_FORMAT; |
| 191 | } |
| 192 | end = p + len; |
| 193 | tbs.len = end - tbs.p; |
| 194 | |
| 195 | /* |
Demi Marie Obenour | 83737b7 | 2022-12-08 15:23:50 -0500 | [diff] [blame] | 196 | * Version ::= [0] EXPLICIT INTEGER { v1(0), v2(1), v3(2) } |
| 197 | * -- only v3 accepted |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 198 | */ |
Demi Marie Obenour | 83737b7 | 2022-12-08 15:23:50 -0500 | [diff] [blame] | 199 | if (((end - p) <= (ptrdiff_t)sizeof(v3)) || |
| 200 | (memcmp(p, v3, sizeof(v3)) != 0)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 201 | return IMG_PARSER_ERR_FORMAT; |
| 202 | } |
Demi Marie Obenour | 83737b7 | 2022-12-08 15:23:50 -0500 | [diff] [blame] | 203 | p += sizeof(v3); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * CertificateSerialNumber ::= INTEGER |
| 207 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 208 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 209 | if (ret != 0) { |
| 210 | return IMG_PARSER_ERR_FORMAT; |
| 211 | } |
| 212 | p += len; |
| 213 | |
| 214 | /* |
| 215 | * signature AlgorithmIdentifier |
| 216 | */ |
| 217 | sig_alg1.p = p; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 218 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 219 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 220 | if (ret != 0) { |
| 221 | return IMG_PARSER_ERR_FORMAT; |
| 222 | } |
| 223 | if ((end - p) < 1) { |
| 224 | return IMG_PARSER_ERR_FORMAT; |
| 225 | } |
| 226 | sig_alg1.len = (p + len) - sig_alg1.p; |
| 227 | p += len; |
| 228 | |
| 229 | /* |
| 230 | * issuer Name |
| 231 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 232 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 233 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 234 | if (ret != 0) { |
| 235 | return IMG_PARSER_ERR_FORMAT; |
| 236 | } |
| 237 | p += len; |
| 238 | |
| 239 | /* |
| 240 | * Validity ::= SEQUENCE { |
| 241 | * notBefore Time, |
| 242 | * notAfter Time } |
| 243 | * |
| 244 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 245 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 246 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 247 | if (ret != 0) { |
| 248 | return IMG_PARSER_ERR_FORMAT; |
| 249 | } |
| 250 | p += len; |
| 251 | |
| 252 | /* |
| 253 | * subject Name |
| 254 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 255 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 256 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 257 | if (ret != 0) { |
| 258 | return IMG_PARSER_ERR_FORMAT; |
| 259 | } |
| 260 | p += len; |
| 261 | |
| 262 | /* |
| 263 | * SubjectPublicKeyInfo |
| 264 | */ |
| 265 | pk.p = p; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 266 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 267 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 268 | if (ret != 0) { |
| 269 | return IMG_PARSER_ERR_FORMAT; |
| 270 | } |
Demi Marie Obenour | b1342a2 | 2022-12-08 15:24:18 -0500 | [diff] [blame] | 271 | pk_end = p + len; |
| 272 | pk.len = pk_end - pk.p; |
| 273 | |
Demi Marie Obenour | 0587d9c | 2022-12-08 15:24:36 -0500 | [diff] [blame] | 274 | /* algorithm */ |
Demi Marie Obenour | b1342a2 | 2022-12-08 15:24:18 -0500 | [diff] [blame] | 275 | ret = mbedtls_asn1_get_tag(&p, pk_end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 276 | MBEDTLS_ASN1_SEQUENCE); |
| 277 | if (ret != 0) { |
| 278 | return IMG_PARSER_ERR_FORMAT; |
| 279 | } |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 280 | p += len; |
| 281 | |
Demi Marie Obenour | 4d1610b | 2022-12-08 15:24:27 -0500 | [diff] [blame] | 282 | /* Key is a BIT STRING and must use all bytes in SubjectPublicKeyInfo */ |
| 283 | ret = mbedtls_asn1_get_bitstring_null(&p, pk_end, &len); |
Demi Marie Obenour | b1342a2 | 2022-12-08 15:24:18 -0500 | [diff] [blame] | 284 | if ((ret != 0) || (p + len != pk_end)) { |
| 285 | return IMG_PARSER_ERR_FORMAT; |
| 286 | } |
| 287 | p = pk_end; |
| 288 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 289 | /* |
| 290 | * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
| 291 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 292 | ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 293 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 294 | MBEDTLS_ASN1_CONSTRUCTED | 1); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 295 | if (ret != 0) { |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 296 | if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 297 | return IMG_PARSER_ERR_FORMAT; |
| 298 | } |
| 299 | } else { |
| 300 | p += len; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
| 305 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 306 | ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 307 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 308 | MBEDTLS_ASN1_CONSTRUCTED | 2); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 309 | if (ret != 0) { |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 310 | if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 311 | return IMG_PARSER_ERR_FORMAT; |
| 312 | } |
| 313 | } else { |
| 314 | p += len; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * extensions [3] EXPLICIT Extensions OPTIONAL |
Demi Marie Obenour | 47a8055 | 2022-12-08 15:23:58 -0500 | [diff] [blame] | 319 | * } |
| 320 | * |
| 321 | * X.509 and RFC5280 allow omitting the extensions entirely. |
| 322 | * However, in TF-A, a certificate with no extensions would |
| 323 | * always fail later on, as the extensions contain the |
| 324 | * information needed to authenticate the next stage in the |
| 325 | * boot chain. Furthermore, get_ext() assumes that the |
| 326 | * extensions have been parsed into v3_ext, and allowing |
| 327 | * there to be no extensions would pointlessly complicate |
| 328 | * the code. Therefore, just reject certificates without |
| 329 | * extensions. This is also why version 1 and 2 certificates |
| 330 | * are rejected above. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 331 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 332 | ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 333 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 334 | MBEDTLS_ASN1_CONSTRUCTED | 3); |
Demi Marie Obenour | fa07d04 | 2022-12-08 15:23:56 -0500 | [diff] [blame] | 335 | if ((ret != 0) || (len != (size_t)(end - p))) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 336 | return IMG_PARSER_ERR_FORMAT; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
Demi Marie Obenour | fa07d04 | 2022-12-08 15:23:56 -0500 | [diff] [blame] | 341 | * -- must use all remaining bytes in TBSCertificate |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 342 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 343 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 344 | MBEDTLS_ASN1_SEQUENCE); |
Demi Marie Obenour | fa07d04 | 2022-12-08 15:23:56 -0500 | [diff] [blame] | 345 | if ((ret != 0) || (len != (size_t)(end - p))) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 346 | return IMG_PARSER_ERR_FORMAT; |
| 347 | } |
Demi Marie Obenour | 0587d9c | 2022-12-08 15:24:36 -0500 | [diff] [blame] | 348 | v3_ext.p = p; |
| 349 | v3_ext.len = len; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 350 | |
| 351 | /* |
Demi Marie Obenour | 47a8055 | 2022-12-08 15:23:58 -0500 | [diff] [blame] | 352 | * Check extensions integrity. At least one extension is |
| 353 | * required: the ASN.1 specifies a minimum size of 1, and at |
| 354 | * least one extension is needed to authenticate the next stage |
| 355 | * in the boot chain. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 356 | */ |
Demi Marie Obenour | 47a8055 | 2022-12-08 15:23:58 -0500 | [diff] [blame] | 357 | do { |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 358 | ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 359 | MBEDTLS_ASN1_CONSTRUCTED | |
| 360 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 361 | if (ret != 0) { |
| 362 | return IMG_PARSER_ERR_FORMAT; |
| 363 | } |
| 364 | |
| 365 | /* Get extension ID */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 366 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 367 | if (ret != 0) { |
| 368 | return IMG_PARSER_ERR_FORMAT; |
| 369 | } |
| 370 | p += len; |
| 371 | |
| 372 | /* Get optional critical */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 373 | ret = mbedtls_asn1_get_bool(&p, end, &is_critical); |
| 374 | if ((ret != 0) && (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 375 | return IMG_PARSER_ERR_FORMAT; |
| 376 | } |
| 377 | |
| 378 | /* Data should be octet string type */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 379 | ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 380 | MBEDTLS_ASN1_OCTET_STRING); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 381 | if (ret != 0) { |
| 382 | return IMG_PARSER_ERR_FORMAT; |
| 383 | } |
| 384 | p += len; |
Demi Marie Obenour | 47a8055 | 2022-12-08 15:23:58 -0500 | [diff] [blame] | 385 | } while (p < end); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 386 | |
| 387 | if (p != end) { |
| 388 | return IMG_PARSER_ERR_FORMAT; |
| 389 | } |
| 390 | |
| 391 | end = crt_end; |
| 392 | |
| 393 | /* |
| 394 | * } |
| 395 | * -- end of TBSCertificate |
| 396 | * |
| 397 | * signatureAlgorithm AlgorithmIdentifier |
Demi Marie Obenour | 139b84c | 2022-12-08 15:24:23 -0500 | [diff] [blame] | 398 | * -- Does not need to be parsed. Ensuring it is bitwise |
| 399 | * -- identical (including the tag!) with the first signature |
| 400 | * -- algorithm is sufficient. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 401 | */ |
Demi Marie Obenour | 139b84c | 2022-12-08 15:24:23 -0500 | [diff] [blame] | 402 | if ((sig_alg1.len >= (size_t)(end - p)) || |
| 403 | (0 != memcmp(sig_alg1.p, p, sig_alg1.len))) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 404 | return IMG_PARSER_ERR_FORMAT; |
| 405 | } |
Demi Marie Obenour | 139b84c | 2022-12-08 15:24:23 -0500 | [diff] [blame] | 406 | p += sig_alg1.len; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 407 | memcpy(&sig_alg, &sig_alg1, sizeof(sig_alg)); |
| 408 | |
| 409 | /* |
| 410 | * signatureValue BIT STRING |
| 411 | */ |
| 412 | signature.p = p; |
Demi Marie Obenour | 4d1610b | 2022-12-08 15:24:27 -0500 | [diff] [blame] | 413 | ret = mbedtls_asn1_get_bitstring_null(&p, end, &len); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 414 | if (ret != 0) { |
| 415 | return IMG_PARSER_ERR_FORMAT; |
| 416 | } |
| 417 | signature.len = (p + len) - signature.p; |
| 418 | p += len; |
| 419 | |
| 420 | /* Check certificate length */ |
| 421 | if (p != end) { |
| 422 | return IMG_PARSER_ERR_FORMAT; |
| 423 | } |
| 424 | |
| 425 | return IMG_PARSER_OK; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | /* Exported functions */ |
| 430 | |
| 431 | static void init(void) |
| 432 | { |
| 433 | mbedtls_init(); |
| 434 | } |
| 435 | |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 436 | /* |
| 437 | * Wrapper for cert_parse() that clears the static variables used by it in case |
| 438 | * of an error. |
| 439 | */ |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 440 | static int check_integrity(void *img, unsigned int img_len) |
| 441 | { |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 442 | int rc = cert_parse(img, img_len); |
| 443 | |
| 444 | if (rc != IMG_PARSER_OK) |
| 445 | clear_temp_vars(); |
| 446 | |
| 447 | return rc; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Extract an authentication parameter from an X509v3 certificate |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 452 | * |
| 453 | * This function returns a pointer to the extracted data and its length. |
| 454 | * Depending on the type of parameter, a pointer to the data stored in the |
| 455 | * certificate may be returned (i.e. an octet string containing a hash). Other |
| 456 | * data may need to be copied and formatted (i.e. integers). In the later case, |
| 457 | * a buffer of the correct type needs to be statically allocated, filled and |
| 458 | * returned. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 459 | */ |
| 460 | static int get_auth_param(const auth_param_type_desc_t *type_desc, |
| 461 | void *img, unsigned int img_len, |
| 462 | void **param, unsigned int *param_len) |
| 463 | { |
| 464 | int rc = IMG_PARSER_OK; |
| 465 | |
| 466 | /* We do not use img because the check_integrity function has already |
| 467 | * extracted the relevant data (v3_ext, pk, sig_alg, etc) */ |
| 468 | |
| 469 | switch (type_desc->type) { |
| 470 | case AUTH_PARAM_RAW_DATA: |
| 471 | /* Data to be signed */ |
| 472 | *param = (void *)tbs.p; |
| 473 | *param_len = (unsigned int)tbs.len; |
| 474 | break; |
| 475 | case AUTH_PARAM_HASH: |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 476 | case AUTH_PARAM_NV_CTR: |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 477 | /* All these parameters are included as X509v3 extensions */ |
| 478 | rc = get_ext(type_desc->cookie, param, param_len); |
| 479 | break; |
| 480 | case AUTH_PARAM_PUB_KEY: |
| 481 | if (type_desc->cookie != 0) { |
| 482 | /* Get public key from extension */ |
| 483 | rc = get_ext(type_desc->cookie, param, param_len); |
| 484 | } else { |
| 485 | /* Get the subject public key */ |
| 486 | *param = (void *)pk.p; |
| 487 | *param_len = (unsigned int)pk.len; |
| 488 | } |
| 489 | break; |
| 490 | case AUTH_PARAM_SIG_ALG: |
| 491 | /* Get the certificate signature algorithm */ |
| 492 | *param = (void *)sig_alg.p; |
| 493 | *param_len = (unsigned int)sig_alg.len; |
| 494 | break; |
| 495 | case AUTH_PARAM_SIG: |
| 496 | /* Get the certificate signature */ |
| 497 | *param = (void *)signature.p; |
| 498 | *param_len = (unsigned int)signature.len; |
| 499 | break; |
| 500 | default: |
| 501 | rc = IMG_PARSER_ERR_NOT_FOUND; |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | return rc; |
| 506 | } |
| 507 | |
| 508 | REGISTER_IMG_PARSER_LIB(IMG_CERT, LIB_NAME, init, \ |
| 509 | check_integrity, get_auth_param); |