Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | /* |
Yann Gautier | bb7ffdf | 2023-01-06 11:47:26 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2023, 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 |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 69 | * in the certificate. OID may be NULL to request that get_ext() |
| 70 | * is only being called for integrity checking. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 71 | */ |
| 72 | static int get_ext(const char *oid, void **ext, unsigned int *ext_len) |
| 73 | { |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 74 | int oid_len, ret, is_critical; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 75 | size_t len; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 76 | unsigned char *p; |
| 77 | const unsigned char *end; |
| 78 | char oid_str[MAX_OID_STR_LEN]; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 79 | mbedtls_asn1_buf extn_oid; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 80 | |
| 81 | p = v3_ext.p; |
| 82 | end = v3_ext.p + v3_ext.len; |
| 83 | |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 84 | /* |
| 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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 92 | |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 93 | 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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 99 | end_ext_data = p + len; |
| 100 | |
| 101 | /* Get extension ID */ |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 102 | 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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 108 | extn_oid.p = p; |
| 109 | p += extn_oid.len; |
| 110 | |
| 111 | /* Get optional critical */ |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 112 | 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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 116 | |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 117 | /* |
| 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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 126 | |
| 127 | /* Detect requested extension */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 128 | oid_len = mbedtls_oid_get_numeric_string(oid_str, |
| 129 | MAX_OID_STR_LEN, |
| 130 | &extn_oid); |
Nicolas Toromanoff | 2d817a3 | 2020-12-23 16:01:25 +0100 | [diff] [blame] | 131 | if ((oid_len == MBEDTLS_ERR_OID_BUF_TOO_SMALL) || (oid_len < 0)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 132 | return IMG_PARSER_ERR; |
| 133 | } |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 134 | |
| 135 | if ((oid != NULL) && |
| 136 | ((size_t)oid_len == strlen(oid_str)) && |
| 137 | (strcmp(oid, oid_str) == 0)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 138 | *ext = (void *)p; |
| 139 | *ext_len = (unsigned int)len; |
| 140 | return IMG_PARSER_OK; |
| 141 | } |
| 142 | |
| 143 | /* Next */ |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 144 | p = end_ext_data; |
| 145 | } while (p < end); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 146 | |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 147 | return (oid == NULL) ? IMG_PARSER_OK : IMG_PARSER_ERR_NOT_FOUND; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | * Check the integrity of the certificate ASN.1 structure. |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 153 | * |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 154 | * Extract the relevant data that will be used later during authentication. |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 155 | * |
| 156 | * This function doesn't clear the static variables located on the top of this |
| 157 | * file in case of an error. It is only called from check_integrity(), which |
| 158 | * performs the cleanup if necessary. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 159 | */ |
| 160 | static int cert_parse(void *img, unsigned int img_len) |
| 161 | { |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 162 | int ret; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 163 | size_t len; |
Demi Marie Obenour | b1342a2 | 2022-12-08 15:24:18 -0500 | [diff] [blame] | 164 | unsigned char *p, *end, *crt_end, *pk_end; |
Demi Marie Obenour | 139b84c | 2022-12-08 15:24:23 -0500 | [diff] [blame] | 165 | mbedtls_asn1_buf sig_alg1; |
Demi Marie Obenour | 83737b7 | 2022-12-08 15:23:50 -0500 | [diff] [blame] | 166 | /* |
| 167 | * The unique ASN.1 DER encoding of [0] EXPLICIT INTEGER { v3(2} }. |
| 168 | */ |
| 169 | static const char v3[] = { |
| 170 | /* The outer CONTEXT SPECIFIC 0 tag */ |
| 171 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0, |
| 172 | /* The number bytes used to encode the inner INTEGER */ |
| 173 | 3, |
| 174 | /* The tag of the inner INTEGER */ |
| 175 | MBEDTLS_ASN1_INTEGER, |
| 176 | /* The number of bytes needed to represent 2 */ |
| 177 | 1, |
| 178 | /* The actual value 2 */ |
| 179 | 2, |
| 180 | }; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 181 | |
| 182 | p = (unsigned char *)img; |
| 183 | len = img_len; |
Demi Marie Obenour | 01ea239 | 2023-01-19 09:50:16 -0500 | [diff] [blame] | 184 | crt_end = p + len; |
| 185 | end = crt_end; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * Certificate ::= SEQUENCE { |
| 189 | * tbsCertificate TBSCertificate, |
| 190 | * signatureAlgorithm AlgorithmIdentifier, |
| 191 | * signatureValue BIT STRING } |
| 192 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 193 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 194 | MBEDTLS_ASN1_SEQUENCE); |
Demi Marie Obenour | 01ea239 | 2023-01-19 09:50:16 -0500 | [diff] [blame] | 195 | if ((ret != 0) || ((p + len) != end)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 196 | return IMG_PARSER_ERR_FORMAT; |
| 197 | } |
| 198 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 199 | /* |
| 200 | * TBSCertificate ::= SEQUENCE { |
| 201 | */ |
| 202 | tbs.p = p; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 203 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 204 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 205 | if (ret != 0) { |
| 206 | return IMG_PARSER_ERR_FORMAT; |
| 207 | } |
| 208 | end = p + len; |
| 209 | tbs.len = end - tbs.p; |
| 210 | |
| 211 | /* |
Demi Marie Obenour | 83737b7 | 2022-12-08 15:23:50 -0500 | [diff] [blame] | 212 | * Version ::= [0] EXPLICIT INTEGER { v1(0), v2(1), v3(2) } |
| 213 | * -- only v3 accepted |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 214 | */ |
Demi Marie Obenour | 83737b7 | 2022-12-08 15:23:50 -0500 | [diff] [blame] | 215 | if (((end - p) <= (ptrdiff_t)sizeof(v3)) || |
| 216 | (memcmp(p, v3, sizeof(v3)) != 0)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 217 | return IMG_PARSER_ERR_FORMAT; |
| 218 | } |
Demi Marie Obenour | 83737b7 | 2022-12-08 15:23:50 -0500 | [diff] [blame] | 219 | p += sizeof(v3); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * CertificateSerialNumber ::= INTEGER |
| 223 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 224 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 225 | if (ret != 0) { |
| 226 | return IMG_PARSER_ERR_FORMAT; |
| 227 | } |
| 228 | p += len; |
| 229 | |
| 230 | /* |
| 231 | * signature AlgorithmIdentifier |
| 232 | */ |
| 233 | sig_alg1.p = p; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 234 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 235 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 236 | if (ret != 0) { |
| 237 | return IMG_PARSER_ERR_FORMAT; |
| 238 | } |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 239 | sig_alg1.len = (p + len) - sig_alg1.p; |
| 240 | p += len; |
| 241 | |
| 242 | /* |
| 243 | * issuer Name |
| 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 | * Validity ::= SEQUENCE { |
| 254 | * notBefore Time, |
| 255 | * notAfter Time } |
| 256 | * |
| 257 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 258 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 259 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 260 | if (ret != 0) { |
| 261 | return IMG_PARSER_ERR_FORMAT; |
| 262 | } |
| 263 | p += len; |
| 264 | |
| 265 | /* |
| 266 | * subject Name |
| 267 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 268 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 269 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 270 | if (ret != 0) { |
| 271 | return IMG_PARSER_ERR_FORMAT; |
| 272 | } |
| 273 | p += len; |
| 274 | |
| 275 | /* |
| 276 | * SubjectPublicKeyInfo |
| 277 | */ |
| 278 | pk.p = p; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 279 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 280 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 281 | if (ret != 0) { |
| 282 | return IMG_PARSER_ERR_FORMAT; |
| 283 | } |
Demi Marie Obenour | b1342a2 | 2022-12-08 15:24:18 -0500 | [diff] [blame] | 284 | pk_end = p + len; |
| 285 | pk.len = pk_end - pk.p; |
| 286 | |
Demi Marie Obenour | 0587d9c | 2022-12-08 15:24:36 -0500 | [diff] [blame] | 287 | /* algorithm */ |
Demi Marie Obenour | b1342a2 | 2022-12-08 15:24:18 -0500 | [diff] [blame] | 288 | ret = mbedtls_asn1_get_tag(&p, pk_end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 289 | MBEDTLS_ASN1_SEQUENCE); |
| 290 | if (ret != 0) { |
| 291 | return IMG_PARSER_ERR_FORMAT; |
| 292 | } |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 293 | p += len; |
| 294 | |
Demi Marie Obenour | 4d1610b | 2022-12-08 15:24:27 -0500 | [diff] [blame] | 295 | /* Key is a BIT STRING and must use all bytes in SubjectPublicKeyInfo */ |
| 296 | ret = mbedtls_asn1_get_bitstring_null(&p, pk_end, &len); |
Demi Marie Obenour | b1342a2 | 2022-12-08 15:24:18 -0500 | [diff] [blame] | 297 | if ((ret != 0) || (p + len != pk_end)) { |
| 298 | return IMG_PARSER_ERR_FORMAT; |
| 299 | } |
| 300 | p = pk_end; |
| 301 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 302 | /* |
| 303 | * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 304 | * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
Demi Marie Obenour | 221a648 | 2023-01-19 09:46:55 -0500 | [diff] [blame] | 305 | * -- technically these contain BIT STRINGs but that is not worth |
| 306 | * -- validating |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 307 | */ |
Demi Marie Obenour | 221a648 | 2023-01-19 09:46:55 -0500 | [diff] [blame] | 308 | for (int i = 1; i < 3; i++) { |
| 309 | ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 310 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 311 | MBEDTLS_ASN1_CONSTRUCTED | i); |
| 312 | /* |
| 313 | * Unique IDs are obsolete, so MBEDTLS_ERR_ASN1_UNEXPECTED_TAG |
| 314 | * is the common case. |
| 315 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 316 | if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Demi Marie Obenour | 221a648 | 2023-01-19 09:46:55 -0500 | [diff] [blame] | 317 | if (ret != 0) { |
| 318 | return IMG_PARSER_ERR_FORMAT; |
| 319 | } |
| 320 | p += len; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 321 | } |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * extensions [3] EXPLICIT Extensions OPTIONAL |
Demi Marie Obenour | 47a8055 | 2022-12-08 15:23:58 -0500 | [diff] [blame] | 326 | * } |
| 327 | * |
| 328 | * X.509 and RFC5280 allow omitting the extensions entirely. |
| 329 | * However, in TF-A, a certificate with no extensions would |
| 330 | * always fail later on, as the extensions contain the |
| 331 | * information needed to authenticate the next stage in the |
| 332 | * boot chain. Furthermore, get_ext() assumes that the |
| 333 | * extensions have been parsed into v3_ext, and allowing |
| 334 | * there to be no extensions would pointlessly complicate |
| 335 | * the code. Therefore, just reject certificates without |
| 336 | * extensions. This is also why version 1 and 2 certificates |
| 337 | * are rejected above. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 338 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 339 | ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 340 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 341 | MBEDTLS_ASN1_CONSTRUCTED | 3); |
Demi Marie Obenour | fa07d04 | 2022-12-08 15:23:56 -0500 | [diff] [blame] | 342 | if ((ret != 0) || (len != (size_t)(end - p))) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 343 | return IMG_PARSER_ERR_FORMAT; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
Demi Marie Obenour | fa07d04 | 2022-12-08 15:23:56 -0500 | [diff] [blame] | 348 | * -- must use all remaining bytes in TBSCertificate |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 349 | */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 350 | ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 351 | MBEDTLS_ASN1_SEQUENCE); |
Demi Marie Obenour | fa07d04 | 2022-12-08 15:23:56 -0500 | [diff] [blame] | 352 | if ((ret != 0) || (len != (size_t)(end - p))) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 353 | return IMG_PARSER_ERR_FORMAT; |
| 354 | } |
Demi Marie Obenour | 0587d9c | 2022-12-08 15:24:36 -0500 | [diff] [blame] | 355 | v3_ext.p = p; |
| 356 | v3_ext.len = len; |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 357 | p += len; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 358 | |
Demi Marie Obenour | e5e03e8 | 2023-01-28 15:15:37 -0500 | [diff] [blame] | 359 | /* Check extensions integrity */ |
| 360 | ret = get_ext(NULL, NULL, NULL); |
| 361 | if (ret != IMG_PARSER_OK) { |
| 362 | return ret; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | end = crt_end; |
| 366 | |
| 367 | /* |
| 368 | * } |
| 369 | * -- end of TBSCertificate |
| 370 | * |
| 371 | * signatureAlgorithm AlgorithmIdentifier |
Demi Marie Obenour | 139b84c | 2022-12-08 15:24:23 -0500 | [diff] [blame] | 372 | * -- Does not need to be parsed. Ensuring it is bitwise |
| 373 | * -- identical (including the tag!) with the first signature |
| 374 | * -- algorithm is sufficient. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 375 | */ |
Demi Marie Obenour | 139b84c | 2022-12-08 15:24:23 -0500 | [diff] [blame] | 376 | if ((sig_alg1.len >= (size_t)(end - p)) || |
| 377 | (0 != memcmp(sig_alg1.p, p, sig_alg1.len))) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 378 | return IMG_PARSER_ERR_FORMAT; |
| 379 | } |
Demi Marie Obenour | 139b84c | 2022-12-08 15:24:23 -0500 | [diff] [blame] | 380 | p += sig_alg1.len; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 381 | memcpy(&sig_alg, &sig_alg1, sizeof(sig_alg)); |
| 382 | |
| 383 | /* |
| 384 | * signatureValue BIT STRING |
Demi Marie Obenour | 01ea239 | 2023-01-19 09:50:16 -0500 | [diff] [blame] | 385 | * } -- must consume all bytes |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 386 | */ |
| 387 | signature.p = p; |
Demi Marie Obenour | 4d1610b | 2022-12-08 15:24:27 -0500 | [diff] [blame] | 388 | ret = mbedtls_asn1_get_bitstring_null(&p, end, &len); |
Demi Marie Obenour | 01ea239 | 2023-01-19 09:50:16 -0500 | [diff] [blame] | 389 | if ((ret != 0) || ((p + len) != end)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 390 | return IMG_PARSER_ERR_FORMAT; |
| 391 | } |
Demi Marie Obenour | 01ea239 | 2023-01-19 09:50:16 -0500 | [diff] [blame] | 392 | signature.len = end - signature.p; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 393 | |
| 394 | return IMG_PARSER_OK; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /* Exported functions */ |
| 399 | |
| 400 | static void init(void) |
| 401 | { |
| 402 | mbedtls_init(); |
| 403 | } |
| 404 | |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 405 | /* |
| 406 | * Wrapper for cert_parse() that clears the static variables used by it in case |
| 407 | * of an error. |
| 408 | */ |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 409 | static int check_integrity(void *img, unsigned int img_len) |
| 410 | { |
Antonio Nino Diaz | 30eb967 | 2017-01-13 15:03:19 +0000 | [diff] [blame] | 411 | int rc = cert_parse(img, img_len); |
| 412 | |
| 413 | if (rc != IMG_PARSER_OK) |
| 414 | clear_temp_vars(); |
| 415 | |
| 416 | return rc; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Extract an authentication parameter from an X509v3 certificate |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 421 | * |
| 422 | * This function returns a pointer to the extracted data and its length. |
| 423 | * Depending on the type of parameter, a pointer to the data stored in the |
| 424 | * certificate may be returned (i.e. an octet string containing a hash). Other |
| 425 | * data may need to be copied and formatted (i.e. integers). In the later case, |
| 426 | * a buffer of the correct type needs to be statically allocated, filled and |
| 427 | * returned. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 428 | */ |
| 429 | static int get_auth_param(const auth_param_type_desc_t *type_desc, |
| 430 | void *img, unsigned int img_len, |
| 431 | void **param, unsigned int *param_len) |
| 432 | { |
| 433 | int rc = IMG_PARSER_OK; |
| 434 | |
| 435 | /* We do not use img because the check_integrity function has already |
| 436 | * extracted the relevant data (v3_ext, pk, sig_alg, etc) */ |
| 437 | |
| 438 | switch (type_desc->type) { |
| 439 | case AUTH_PARAM_RAW_DATA: |
| 440 | /* Data to be signed */ |
| 441 | *param = (void *)tbs.p; |
| 442 | *param_len = (unsigned int)tbs.len; |
| 443 | break; |
| 444 | case AUTH_PARAM_HASH: |
Juan Castillo | bfb7fa6 | 2016-01-22 11:05:57 +0000 | [diff] [blame] | 445 | case AUTH_PARAM_NV_CTR: |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 446 | /* All these parameters are included as X509v3 extensions */ |
| 447 | rc = get_ext(type_desc->cookie, param, param_len); |
| 448 | break; |
| 449 | case AUTH_PARAM_PUB_KEY: |
Yann Gautier | bb7ffdf | 2023-01-06 11:47:26 +0100 | [diff] [blame] | 450 | if (type_desc->cookie != NULL) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 451 | /* Get public key from extension */ |
| 452 | rc = get_ext(type_desc->cookie, param, param_len); |
| 453 | } else { |
| 454 | /* Get the subject public key */ |
| 455 | *param = (void *)pk.p; |
| 456 | *param_len = (unsigned int)pk.len; |
| 457 | } |
| 458 | break; |
| 459 | case AUTH_PARAM_SIG_ALG: |
| 460 | /* Get the certificate signature algorithm */ |
| 461 | *param = (void *)sig_alg.p; |
| 462 | *param_len = (unsigned int)sig_alg.len; |
| 463 | break; |
| 464 | case AUTH_PARAM_SIG: |
| 465 | /* Get the certificate signature */ |
| 466 | *param = (void *)signature.p; |
| 467 | *param_len = (unsigned int)signature.len; |
| 468 | break; |
| 469 | default: |
| 470 | rc = IMG_PARSER_ERR_NOT_FOUND; |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | return rc; |
| 475 | } |
| 476 | |
Elyes Haouas | 183638f | 2023-02-13 10:05:41 +0100 | [diff] [blame] | 477 | REGISTER_IMG_PARSER_LIB(IMG_CERT, LIB_NAME, init, |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 478 | check_integrity, get_auth_param); |